I try show errors without fieldname
identifier. For this i run in template {{ form.non_field_errors }}
. But error not displayed. If i run with form.errors
the erros are displayed, but with fieldname
too, it's not what i want.
I use AbstractBaseUser
like:
models.py
class User(AbstractBaseUser):
username = models.CharField(
verbose_name = u'Nome',
max_length=33
)
#identifier
email = models.EmailField(
verbose_name = u'Email',
max_length=255,
unique=True,
)
phone = models.CharField(
verbose_name = u'telefone',
max_length=12
)
#...
forms.py
class SignupForm(UserCreationForm):
username = forms.CharField(
label=False,
max_length=100,
required=True,
widget=forms.TextInput(
attrs={
'placeholder': 'Nome',
'autocomplete':'off',
}
))
email = forms.EmailField(
label=False,
required=True,
widget=forms.TextInput(
attrs={'placeholder': 'Email'}
))
email_confirmation = forms.EmailField(
required=True,
widget=forms.TextInput(
attrs={
'placeholder': 'Confirme o email',
'id': 'email_confirmation',
'class': 'input cemail-cad'
}
))
phone = forms.CharField(
label=False,
required=True,
widget=forms.TextInput(
attrs={'placeholder': 'Telefone'}
))
password1 = forms.CharField(
label=False,
max_length=50,
required=True,
widget=forms.PasswordInput(
attrs={'placeholder': 'Senha'}
))
password2 = forms.CharField(
label=False,
max_length=50,
required=True,
widget=forms.PasswordInput(
attrs={'placeholder': 'Confirme sua senha'}
))
class Meta:
model = User
fields = ['username', 'email', 'phone', 'password1', 'password2']
def clean_username(self):
# mínimo 2 caracteres máximo 150 caracteres
if not re.compile(r'^\S*[\w\.\-\_\d]{3,150}').match(str(self)):
raise ValidationError("Nome inválido: Mínimo 3 caracteres, somente letras, números e '.', '-' ou '_' especiais")
def clean_email(self):
cleaned_data = super(SignupForm, self).clean()
email = cleaned_data.get('email')
email_confirmation = cleaned_data.get('email_confirmation')
if email != email_confirmation:
raise forms.ValidationError("Email diferente")
if User.objects.filter(email__iexact=email, is_active=True).exists():
raise ValidationError('Email já exite')
def clean_phone(self):
if not re.compile(r'([0-9]{2})([0-9]{5})([0-9]{4})').match(str(self)):
raise ValidationError("Telefone inválido: 00-00000-0000 somente números com DDD ")
views.py
class SignupView(CreateView):
form_class = SignupForm
template_name = 'admin/signup.html'
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
return redirect(to='/confirma-conta/')
else:
return render(request, self.template_name, {'form': form})
templates/admin/signup.html
{% if form.errors %}
<ul ><li>{{ form.non_field_errors }}</li></ul>
{% endif %}
{{ form.username }}
{{ form.email }}
{{ form.email_confirmation }}
{{ form.phone }}
{{ form.password1 }}
{{ form.password2 }}
urls.py
urlpatterns = [
path('criar-conta/', SignupView.as_view(), name='signup'),
#...
]
What is need to or how form.non_field_errors
works?
CodePudding user response:
Form.non_field_errors()
should give you a list of errors:
{% if form.non_field_errors %}
<ul>
{% for error in form.non_field_errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}