如何向@Assert 添加消息?
How to Add message to the @Assert?
我正在尝试自定义 Symfony 验证器消息!
* @UniqueEntity(fields={"email"}, message="The Email is already used.")
唯一的实体错误消息工作得很好,显示如下
例如,我尝试向 * @Assert\NotBlank()
添加一条消息并执行 @Assert\NotBlank(message="The password can't be blank ")
但是它显示是这样的..
我希望错误信息是密码不能为空而不是Veuillez compléter ce champ.
此外,我希望它看起来像 UniqueEntity 消息!
怎么样?
我的实体代码:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @UniqueEntity(
* fields={"username"},
* message="The username is already used."
* )
* @UniqueEntity(
* fields={"email"},
* message="The Email is already used."
* )
*/
class User
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255 , unique=true)
* @Assert\NotBlank()
*/
private $username;
/**
* @ORM\Column(type="string", length=255 , unique=true)
* @Assert\NotBlank()
* @Assert\Email(message="This Email is not valid ")
*/
private $email;
/**
* @ORM\Column(type="string", length=255 , unique=true)
* @Assert\NotBlank()
*/
private $password;
/**
* @ORM\Column(type="string", length=50, nullable=true)
* @Assert\Regex(
* pattern="/[0-9]{8}/"
* )
*/
private $phone;
/**
* @ORM\Column(type="string", length=255)
*/
private $picture;
/**
* @ORM\Column(type="string", length=255)
*/
private $braclet_id;
/**
* @ORM\Column(type="boolean")
*/
private $is_doctor;
/**
* @Assert\Length(min=5, max=100)
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
*/
private $Full_Name;
}
视图代码
{{ 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) }}
感谢留言评论解决!
如果有人遇到此错误,您可以使用 novalidate attribute
禁用浏览器验证!
而不是
{{ form_start(form) }}
使用
{{ form_start(form, {attr: {novalidate: 'novalidate'}}) }}
我正在尝试自定义 Symfony 验证器消息!
* @UniqueEntity(fields={"email"}, message="The Email is already used.")
唯一的实体错误消息工作得很好,显示如下
例如,我尝试向 * @Assert\NotBlank()
添加一条消息并执行 @Assert\NotBlank(message="The password can't be blank ")
但是它显示是这样的..
我希望错误信息是密码不能为空而不是Veuillez compléter ce champ.
此外,我希望它看起来像 UniqueEntity 消息! 怎么样?
我的实体代码:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @UniqueEntity(
* fields={"username"},
* message="The username is already used."
* )
* @UniqueEntity(
* fields={"email"},
* message="The Email is already used."
* )
*/
class User
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255 , unique=true)
* @Assert\NotBlank()
*/
private $username;
/**
* @ORM\Column(type="string", length=255 , unique=true)
* @Assert\NotBlank()
* @Assert\Email(message="This Email is not valid ")
*/
private $email;
/**
* @ORM\Column(type="string", length=255 , unique=true)
* @Assert\NotBlank()
*/
private $password;
/**
* @ORM\Column(type="string", length=50, nullable=true)
* @Assert\Regex(
* pattern="/[0-9]{8}/"
* )
*/
private $phone;
/**
* @ORM\Column(type="string", length=255)
*/
private $picture;
/**
* @ORM\Column(type="string", length=255)
*/
private $braclet_id;
/**
* @ORM\Column(type="boolean")
*/
private $is_doctor;
/**
* @Assert\Length(min=5, max=100)
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
*/
private $Full_Name;
}
视图代码
{{ 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) }}
感谢留言评论解决!
如果有人遇到此错误,您可以使用 novalidate attribute
禁用浏览器验证!
而不是
{{ form_start(form) }}
使用
{{ form_start(form, {attr: {novalidate: 'novalidate'}}) }}