Home > Software design >  Django form validation
Django form validation

Time:12-28

Is there an easier way to write validation for each item in a form? Maybe embed them in model declaration itself?

class InfoUpdateForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = [ 
                   'first_name',
                   'middle_name',
                   'last_name'
                 ]

    def clean(self):
        cleaned_data = super().clean()

        first_name = cleaned_data.get('first_name')
        if not str(first_name).isalnum():
            self._errors['first_name'] = self.error_class(['First name should constain only alpha numeric characters'])           
        middle_name = cleaned_data.get('middle_name')
        if not str(middle_name).isalnum():
            self._errors['middle_name'] = self.error_class(['Middle name should constain only alpha numeric characters'])           
        last_name = cleaned_data.get('last_name')
        if not str(last_name).isalnum():
            self._errors['last_name'] = self.error_class(['Last name should constain only alpha numeric characters'])  

CodePudding user response:

If you want to keep the ModelForm one way is to move the validation to models.py file (as you thought) and write custom validator:

validators.py

from django.core.exceptions import ValidationError

def is_alpha_numeric(value):
    if not str(value).isalnum():
        raise ValidationError(
            ('%(value)s is not alpha-numeric'),
            params={'value': value},
        )

models.py:

from .validators import is_alpha_numeric

class Profile(models.Model):
    first_name = models.CharField(max_length=100, validators=[is_alpha_numeric])
    middle_name = models.CharField(max_length=100, validators=[is_alpha_numeric])
    last_name = models.CharField(max_length=100, validators=[is_alpha_numeric])

forms.py:

class InfoUpdateForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = [ 
                   'first_name',
                   'middle_name',
                   'last_name'
                 ]
  • Related