Skip to content
Advertisement

How to Add message to the @Assert?

I am trying to customize Symfony validator messages!

 * @UniqueEntity(fields={"email"}, message="The Email is already used.")

The unique entity error message works perfectly fine and shows up like this enter image description here

I tried to add a message to * @AssertNotBlank() for example and do @AssertNotBlank(message="The password can't be blank ") but it shows like this ..

enter image description here

I want the error message to be The password can’t be blank instead of Veuillez compléter ce champ.

Also, I want it to appear like UniqueEntity message! how?

Code of my entity :

<?php

namespace AppEntity;

use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;

/**
 * @ORMEntity(repositoryClass="AppRepositoryUserRepository")
 * @UniqueEntity(
 *     fields={"username"},
 *     message="The username is already used."
 * )
 * @UniqueEntity(
 *     fields={"email"},
 *     message="The Email is already used."
 * )
 */
class User
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMColumn(type="string", length=255 ,  unique=true)
     * @AssertNotBlank()
     */
    private $username;

    /**
     * @ORMColumn(type="string", length=255 , unique=true)
     * @AssertNotBlank()
     * @AssertEmail(message="This Email is not valid ")
     */
    private $email;

    /**
     * @ORMColumn(type="string", length=255 ,  unique=true)
     * @AssertNotBlank()
     */
    private $password;

    /**
     * @ORMColumn(type="string", length=50, nullable=true)
     * @AssertRegex(
     * pattern="/[0-9]{8}/"
     * )
     */
    private $phone;

    /**
     * @ORMColumn(type="string", length=255)
     */
    private $picture;

    /**
     * @ORMColumn(type="string", length=255)
     */
    private $braclet_id;

    /**
     * @ORMColumn(type="boolean")
     */
    private $is_doctor;

    /**
     * @AssertLength(min=5, max=100)
     * @ORMColumn(type="string", length=255)
     * @AssertNotBlank()
     */
    private $Full_Name;
}

Code of View

{{ form_start(form) }}


<div class="col-sm-3">
    <div class="form-group">
        <div class="form-line">
            {{ form_widget(form.password, {'attr': {'class': 'form-control', 'placeholder': 'Password','type': 'Password'} }) }}        
        </div>
    </div>
</div>
<div class="col-sm-3">
    <div class="form-group">
        <div class="error">{{ form_errors(form.password) }} </div>
    </div>
</div> 

{{ form_rest(form) }}
{{ form_end(form) }}

Advertisement

Answer

Solved thanks to msg comment!

In case anyone faced this error u can use novalidate attribute to disable browser validation!

instead of

{{ form_start(form) }}

use

{{ form_start(form, {attr: {novalidate: 'novalidate'}}) }}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement