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 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.15.tar.gz (37.8 kB view details)

Uploaded Source

Built Distribution

edc_form_validators-0.3.15-py3-none-any.whl (43.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: edc-form-validators-0.3.15.tar.gz
  • Upload date:
  • Size: 37.8 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.15.tar.gz
Algorithm Hash digest
SHA256 c0c94d8dfa4e2ff079328941e0a02b471e2c22ce6da6d930f3c3d2f8cd689e7d
MD5 0a7494573ea879ac5fe9d2dca18cb05f
BLAKE2b-256 e62fe123ceb044731661cbbb4066b3aff435ba00aaf03664c7af192edfbcc73b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for edc_form_validators-0.3.15-py3-none-any.whl
Algorithm Hash digest
SHA256 80c779a69dedc8066490d0c0f4a28d716fcddde4410acd570af0f02782465906
MD5 e62fbca68e3eb6828174c25ddc177922
BLAKE2b-256 c54af65e1c47a59557efbdb3b588482a2ddd0393f11f43026d9e30c3c94cf518

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