Skip to main content

Form validator classes for common patterns in django ModelForms.

Project description

pypi actions codecov downloads

edc-form-validators

Form validator classes for ModelForms

ModelForm FormValidator

FormValidator simplifies common patterns used in ModelForm.clean. For example, if there is a response to field A then there should not a be response to field B and visa-versa.

Declare a form with it’s form_validator class and use FormValidatorMixin:

class MyFormValidator(FormValidator):

    def clean(self):
        self.required_if(
            YES,
            field='f1',
            field_required='f2')
        ...

class MyModelForm(FormValidatorMixin, forms.ModelForm):

    form_validator_cls = MyFormValidator

    class Meta:
        model = TestModel
        fields = '__all__'

Testing

Test the form_validator without having to instantiate the ModelForm:

def test_my_form_validator(self):
    options = {
        'f1': YES,
        'f2': None}
    form_validator = MyFormValidator(cleaned_data=options)
    self.assertRaises(ValidationError, form_validator.validate)
    self.assertIn('f2', form_validator._errors)

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

edc-form-validators-0.3.34.tar.gz (41.1 kB view details)

Uploaded Source

Built Distribution

edc_form_validators-0.3.34-py3-none-any.whl (47.0 kB view details)

Uploaded Python 3

File details

Details for the file edc-form-validators-0.3.34.tar.gz.

File metadata

  • Download URL: edc-form-validators-0.3.34.tar.gz
  • Upload date:
  • Size: 41.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for edc-form-validators-0.3.34.tar.gz
Algorithm Hash digest
SHA256 3687f852e625902549e0e646b113f21caa06903feb634b2f1a02b3f1a793af1f
MD5 40287963c3d2b2cc0d3c752877b7ad26
BLAKE2b-256 8da484540b979a91e5425264c0b02f1b1f67d4d4892f2e56cbadb0bfbaf8877f

See more details on using hashes here.

File details

Details for the file edc_form_validators-0.3.34-py3-none-any.whl.

File metadata

File hashes

Hashes for edc_form_validators-0.3.34-py3-none-any.whl
Algorithm Hash digest
SHA256 b0f3e7cfcc044da37ba8be4d1f534ba681380c09bff20cd7e1981fbed0110d96
MD5 73bf9b02a38e191495b544b8300c6de0
BLAKE2b-256 f29a59f41010c030622d3aaacb99d70e3e179a9c1afb8194f118734c8852c13f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page