how to reuse custom form validation rules in other forms in django

Atma

I have the following custom validation for a combination of fields I use throughout my site:

class MyModelForm(forms.ModelForm):
    my_field_type_constant = 'my_field_type'
    my_field_code_constant = 'my_field_code'

    class Meta:
        model = MyModel

    def validate_my_field_code(self, my_field_code_error, my_field_type, my_field_code):

        validate = URLValidator(verify_exists=False)
        try:
            validate(my_field_code)
        except ValidationError:
            messsge = u"My custom error"
            self._errors[my_field_code_error] = self.error_class([messsge])


    def clean(self):

        cleaned_data = super(MyModelForm, self).clean()
        my_field_type = cleaned_data.get(self.my_field_type_constant)
        my_field_code = cleaned_data.get(self.my_field_code_constant)

        my_field_type_from_model = other_model.models.my_field_TYPES[1][0]
        if my_field_type == my_field_type_from_model:
            self.validate_my_field_code(self.my_field_code_constant, my_field_type, my_field_code)
        return cleaned_data

I use the combination of my_field_type and my_field_code in many of the forms in my site. I want to adhere to the DRY principle. How do I make the validation for these fields available to other forms without copying and pasting it into all the other forms?

professorDante

Why not just subclass your other ModelForm classes that will use this validation:

class MyOtherModelForm(MyModelForm):
    pass

Then you get it all for free though inheritance.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

angularJs - Custom validation rules for form

From Dev

Laravel Form Request Array Validation Custom Rules

From Dev

How add Custom Validation Rules when using Form Request Validation in Laravel 5

From Dev

Yii2 - Form validation rules: How to use custom validation function or alternative?

From Dev

django custom form validation error

From Dev

Django custom form validation in ListView

From Dev

How to write a Custom field Validation for ModelSerializers in Django Rest Framework(DRF) similar to Form Validation in Django?

From Dev

How to test custom validation rules in Laravel 5?

From Dev

Angular2 template driven forms: how to create field validation for a custom form control?

From Dev

Laravel form validation if or rules

From Dev

Custom Form Validation in Django-Allauth

From Dev

django 1.8 custom validation of form not working

From Dev

How to reuse custom validation logic in multiple fields from the same Domain

From Dev

How to reuse custom validation logic in multiple fields from the same Domain

From Dev

Django forms - how to override field validation

From Dev

Jasmine: How can I reuse other matchers in my custom matchers

From Java

How to add custom validation to an AngularJS form?

From Dev

codeigniter form validation rules url

From Dev

codeigniter rules group form validation

From Dev

codeigniter rules group form validation

From Dev

How to define forms with a range and other strings in Django?

From Dev

How to exclude some fields in validation of Form in Django

From Dev

Django how to override form validation for a required field

From Dev

How to reuse custom events

From Dev

How to reuse Custom UICollectionView?

From Dev

Django allauth - using custom form Validation errors do not show on the template

From Dev

how to provide a custom name for widget in django forms

From Dev

Custom placeholders for custom validation rules in Laravel 5

From Dev

How to create custom validation for Modelless Forms in cakephp3

Related Related

  1. 1

    angularJs - Custom validation rules for form

  2. 2

    Laravel Form Request Array Validation Custom Rules

  3. 3

    How add Custom Validation Rules when using Form Request Validation in Laravel 5

  4. 4

    Yii2 - Form validation rules: How to use custom validation function or alternative?

  5. 5

    django custom form validation error

  6. 6

    Django custom form validation in ListView

  7. 7

    How to write a Custom field Validation for ModelSerializers in Django Rest Framework(DRF) similar to Form Validation in Django?

  8. 8

    How to test custom validation rules in Laravel 5?

  9. 9

    Angular2 template driven forms: how to create field validation for a custom form control?

  10. 10

    Laravel form validation if or rules

  11. 11

    Custom Form Validation in Django-Allauth

  12. 12

    django 1.8 custom validation of form not working

  13. 13

    How to reuse custom validation logic in multiple fields from the same Domain

  14. 14

    How to reuse custom validation logic in multiple fields from the same Domain

  15. 15

    Django forms - how to override field validation

  16. 16

    Jasmine: How can I reuse other matchers in my custom matchers

  17. 17

    How to add custom validation to an AngularJS form?

  18. 18

    codeigniter form validation rules url

  19. 19

    codeigniter rules group form validation

  20. 20

    codeigniter rules group form validation

  21. 21

    How to define forms with a range and other strings in Django?

  22. 22

    How to exclude some fields in validation of Form in Django

  23. 23

    Django how to override form validation for a required field

  24. 24

    How to reuse custom events

  25. 25

    How to reuse Custom UICollectionView?

  26. 26

    Django allauth - using custom form Validation errors do not show on the template

  27. 27

    how to provide a custom name for widget in django forms

  28. 28

    Custom placeholders for custom validation rules in Laravel 5

  29. 29

    How to create custom validation for Modelless Forms in cakephp3

HotTag

Archive