0

I'm trying to customize the display of this error message "There is already an account with this" in Twig.

This is the relevant code in the form class:

->add('cin', TextType::class, [
            'required' => true,
        ])

And here is the relvant HTML/Twig code:

<div class="form-outline">
   {{ form_row(registrationForm.cin ,{'label':false,'attr':{'placeholder':'Numéro carte d\'identité', 'name':'cin', 'class':'form-control', 'id':'cin', 'type':'number', 'minlength':'8', 'maxlength':'8', 'required data-error':'Veuillez saisir votre numéro de carte d\'identité', } } ) }}
   <span style="color: red">{{ form_errors(registrationForm.cin) }}</span>
</div>

This is what I get on my screen in case there is an error:

enter image description here

However, What I would like to get is only the red message (that is below the text field) customized to something like "Invalid card ID", for instance. So, what's wrong in my code? Any idea?

DarkBee
  • 16,592
  • 6
  • 46
  • 58
Nadim
  • 382
  • 1
  • 7
  • 29

2 Answers2

1

After some more research we identified that the error message can be changed in the @UniqueEntity validator in the user class.

Changing the current validator from

/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @UniqueEntity(fields={"cin"}, message="There is already an account with this cin")
*/

to

/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @UniqueEntity(fields={"cin"}, message="Invalid card id")
*/

results in the validation error message acutally being modified accordingly.

David
  • 184
  • 8
  • Superb! Now, there is still one more thing to figure out: How can I get rid of the error message duplication? – Nadim Jan 01 '22 at 21:36
  • See [Customize Form Rendering](https://symfony.com/doc/4.4/form/form_customization.html) for more information on why your template renders the error twice. – David Jan 01 '22 at 22:47
  • I found it! I have to change `form_row` to `form_widget` – Nadim Jan 01 '22 at 22:58
0

Since TextType extends the FormType class you should be able to define a custom invalid message attribute. Symfony will try to resolve the given identificator by searching in the validator translations or just use the given text if a matching translation can not be found. See FormType Field for more information.

->add('cin', TextType::class, [
    'required' => true,
    'invalid_message' => 'Invalid card ID'
])

Changing the invalid message to this will result in twig rendering the custom message. In the example above I just added the required parameter as an option to the TextType.

David
  • 184
  • 8
  • I've tried that, but nothing has changed. It keeps giving me the same reuslt. – Nadim Jan 01 '22 at 20:06
  • Not quite sure how Symfony/Twig handles merging of configuration options. Keep the change and try to change the rendering of the type just to ``{{ form_row(registrationForm.cin) }}``. Does this resolve your problem? – David Jan 01 '22 at 20:17
  • I've also tried that, but the bug still persists. – Nadim Jan 01 '22 at 20:23
  • Did you create this form with the MakerBundler? To me it looks like you are trying to use the registration form to add another user and used the cin as the unique identifier. Maybe this direction looks more promesing. – David Jan 01 '22 at 20:33
  • Indeed, the cin is a unique identifier for user registration. In order to create the form in question, I've actually used this command line : `php bin/console make:form` – Nadim Jan 01 '22 at 20:37
  • 1
    Did you add the custom validation ``@UniqueEntity`` to the cin attribute in your users class? – David Jan 01 '22 at 20:50