I don't know how to show one form error one after the other when fields are empty : it worked for email, but not for password, gender, ect... I believe it comes from the condition I created because I get the proper error message when I remove them and paste them just outside the condition.
Where is be the mistake ?
register.html.twig
{% extends 'base.html.twig' %}
{% block main %}
{{ form_start(userform, {'attr': {'novalidate': 'novalidate'}} ) }}
{% set formErrors = userform.vars.errors.form.getErrors(true) %}
{{ dump(userform.vars.value.password|length) }}
{% if formErrors|length %}
<div role="alert">
{% if userform.vars.value.email == null or userform.vars.value.email != 'email' or userform.vars.value.email != 'unique' %}
{{ form_errors(userform.email) }}
{% elseif userform.vars.value.password|length < 6 %}
{{ form_errors(userform.password.first) }}
{% elseif userform.vars.value.gender == null or (userform.vars.value.gender != 'male' and userform.vars.value.gender != 'female' and userform.vars.value.gender != 'non-binary') %}
{{ form_errors(userform.gender) }}
{% elseif userform.vars.value.firstname|length < 2 %}
{{ form_errors(userform.firstname) }}
{% elseif userform.vars.value.lastname|length < 2 %}
{{ form_errors(userform.lastname) }}
{% elseif userform.vars.value.birthdate == null %}
{{ form_errors(userform.birthdate) }}
{% elseif userform.vars.value.occupation|length < 2 %}
{{ form_errors(userform.occupation) }}
{% elseif userform.vars.value.nationality == null %}
{{ form_errors(userform.nationality) }}
{% elseif userform.vars.value.nativelanguage == null %}
{{ form_errors(userform.nativelanguage) }}
{% endif %}
</div>
{% endif %}
CodePudding user response:
{{ form_errors(form) }} is not to do test if field is empty or not or is valid or not but just to display validations messages from Symfony Validator such Assert.
For exmple when you say:
{% if userform.vars.value.email == null %}
yes the first time when you comme on the url of form , the email is always null but this not how we test if email is blank, we don't test it in twig.
Keep in mind that {{ form_errors(form) }} is just for displaying errors messages, just for showing
Let's take this example :
class User implements UserInterface, \Serializable, PasswordAuthenticatedUserInterface
{
#[Assert\NotBlank(message: 'Email ne doit pas être vide')]
#[Assert\Email(message: 'Email n\'est pas valide')]
private ?string $email;
#[Assert\NotBlank]
#[Assert\Length(min: 3, max: 32, minMessage: 'msg min ...', maxMessage: 'msg max ....')]
private $plainPassword;
In User class we have already added a validation test if email is empty or is not valid usnig Assert.
The role of form_erros in twig is just showing this errors messages if they exists but not testing validation
So what you can do in twig is to test if form is valid or not, if not valid so display erros:
{% if not userform.vars.valid %}
{{ form_errors(userform.email) }} // if email is empty or not valid, it will show the message of Assert in user class
{{ form_errors(userform.firstName) }}
{{ form_errors(userform.lastName) }}
{{ form_errors(userform.plainPassword.first) }}
{% endif %}