0

I need to add some validation constraints on my CustomUserCreationForm Meta class.

I can't find where and how to add it. Should I use a RegexValidator or not for this purpose?

For example I'd like the first_name to be minimum length 2 and maximum length 30 only letters.


from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.forms import ModelForm

from .models import *

import datetime


class CustomUserCreationForm(UserCreationForm):
    birth_date = forms.DateField(
        label='birthdate', 
        widget=forms.DateInput(
            attrs={'placeholder':'jj/mm/aaaa'}
        )
    )
    introduction = forms.CharField(
        label='introduction', 
        min_length=20, 
        max_length=500,
        widget = forms.Textarea(
            attrs={
                'placeholder': 'Hey, I\'m Wely.',
                'cols':"40", 
                'rows':"5"
            }
        )
    )
    address1 = forms.CharField(
        label='Address (1st line)', 
        min_length=2, 
        max_length=40,
        widget = forms.TextInput(
            attrs={'placeholder': '1st av. Lincoln'}
            )
        )
    address2 = forms.CharField(
        required=False, 
        label='Address (2nd line)', 
        min_length=0, 
        max_length=40,
        widget = forms.TextInput(
            attrs={'placeholder': 'Apt 3'}
        )
    )
    registration_video = forms.CharField(
        label='video',
        widget = forms.FileInput(
            attrs={'style': 'display:none'}
        )
    )


    class Meta:
        model = CustomUser
        fields = ('first_name', 'last_name', 'email')
        labels = {
            'first_name': 'First name',
            'last_name': 'Last Name',
            'email': 'E-mail address'
        }
        widgets = {
            'first_name': forms.TextInput(attrs = {'placeholder': 'Alicia'}),
            'last_name': forms.TextInput(attrs = {'placeholder': 'Rodriguez'}),
            'email': forms.EmailInput(attrs = {'placeholder': 'alicia@mail.com'}),
        }


Welyweloo
  • 88
  • 8

1 Answers1

0

Not use RegexValidator. Only change like this. Here we add minlength and maxlength inside the attr for first_name field.

class Meta:
        model = CustomUser
        fields = ('first_name', 'last_name', 'email')
        labels = {
            'first_name': 'First name',
            'last_name': 'Last Name',
            'email': 'E-mail address'
        }
        widgets = {
            'first_name': forms.TextInput(attrs = {'placeholder': 'Alicia','minlength':2,'maxlength':30}),
            'last_name': forms.TextInput(attrs = {'placeholder': 'Rodriguez'}),
            'email': forms.EmailInput(attrs = {'placeholder': 'alicia@mail.com'}),
        }
Riyas Ac
  • 1,553
  • 1
  • 9
  • 23
  • Oups I thought I answered. Thank you for your answer. Unfortunately I am looking for a way to do it without touching the attributes. I thought it was possible at least. – Welyweloo Sep 07 '20 at 14:39