Skip to main content

Form validator classes for 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 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.9.tar.gz (29.4 kB view details)

Uploaded Source

Built Distribution

edc_form_validators-0.3.9-py3-none-any.whl (37.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: edc-form-validators-0.3.9.tar.gz
  • Upload date:
  • Size: 29.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.9

File hashes

Hashes for edc-form-validators-0.3.9.tar.gz
Algorithm Hash digest
SHA256 eea5b4a7377a5892859b2327d41c7dc057d2b9e6a638727fd921c0c20a5df8c8
MD5 c49af3cea168ce0212d63ec255aed03a
BLAKE2b-256 4d0a9edc7d98438bb33fe04f3e34249170573bd1e6d3d277d20aeb3beb422c2b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for edc_form_validators-0.3.9-py3-none-any.whl
Algorithm Hash digest
SHA256 9dea3930eccdbe7b24344a045714b58cefcc2291e8823de0446f202e423fcbcb
MD5 6a6549e3667c7b201449f7173598ac1a
BLAKE2b-256 8abd6d6107d7f65a7b325c68feba36a1a68014da3413295d7b4ddd1872127c70

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