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

This version

0.3.6

Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

edc_form_validators-0.3.6-py3-none-any.whl (34.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: edc_form_validators-0.3.6-py3-none-any.whl
  • Upload date:
  • Size: 34.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for edc_form_validators-0.3.6-py3-none-any.whl
Algorithm Hash digest
SHA256 9e1741a6a7edf9b5b2b6e9c70bfc5ab416504a8fb0f57963929732d4b2139111
MD5 86e3cd1a06b6f61bc26dcd1efd5c25ac
BLAKE2b-256 234a87dd14a03e7878de83de9058b898744ba336546c9ed8f802ce5821506f25

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