Skip to main content

Form validator classes for django ModelForms

Project description

pypi travis coverage

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

Built Distribution

edc_form_validators-0.1.11-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file edc-form-validators-0.1.11.macosx-10.13-x86_64.tar.gz.

File metadata

  • Download URL: edc-form-validators-0.1.11.macosx-10.13-x86_64.tar.gz
  • Upload date:
  • Size: 24.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.1.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0

File hashes

Hashes for edc-form-validators-0.1.11.macosx-10.13-x86_64.tar.gz
Algorithm Hash digest
SHA256 3c18b2195dbd3ffec909b4304bcdf4a34796cb71ac528275c7f4e9a4d2e7bb5a
MD5 d1e79438170c5b3ea3cf083d8f1bcd10
BLAKE2b-256 024f6d883817f2ea7ce74710cc733bdf82e5cc10018c057d3419fe866678b843

See more details on using hashes here.

File details

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

File metadata

  • Download URL: edc_form_validators-0.1.11-py3-none-any.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.1.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0

File hashes

Hashes for edc_form_validators-0.1.11-py3-none-any.whl
Algorithm Hash digest
SHA256 4a41c31415ff4b774af912b25b2634ba6dbdf2bc0973267d6825a6def77ed68d
MD5 e3398a7516385c83ae604eca40cdc950
BLAKE2b-256 a6146f49a29d7ea61cb7e140a317e5ed1823b5b31f65383ec3a3d68f81c7dfa2

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