dongyi4420 2016-01-08 09:26
浏览 53

自定义验证消息不适用于Symfony 3

I have the following declaration in my user class (Symfony 3, it's an entity class):

/**
 * @ORM\Column(type="string", length=128, unique=true)
 * @Assert\NotBlank (
 *  groups={"registration"}, 
 *  message = "test"
 * )
 * @Assert\Email(
 *  message = "The email '{{ value }}' is not a valid email.",
 *  groups={"registration"}
 * )
 */
private $email;

Everything works fine, except that I can't set a message, instead of my message ('test' for example) a default "This value should not be blank" is displayed.

Yes, I do have

framework: 
    validation:      { enable_annotations: true }

In my config.yml

Edit: and yes I cleared cache by executing:

sudo php bin/console cache:clear

Edit2: Please note that it actually DOES go through validation, Symfony sees that the email is required, it displays a message if I leave the field empty, it's just that it is not my message, Symfony somehow doesn't see the "message" part.

Edit3: Now, after removing the groups={"registration"} part, as well as changing (in controller)

$errors = $validator->validate($user, null, array('registration'));

to

$errors = $validator->validate($user, null);

(removed array('registration'))

the correct message is displayed. However this is not a solution. I need to use groups.

Edit 4: I'll get crazy...

If I enter no email, and then:

$validator = $this->get('validator');
$errors = $validator->validate($user, null, array('registration'));
dump($errors);

(Notice the "dump" function call) then I see an incorect message displayed on a page (This value should not be blank) but... when I enter the profiler->debug page (the dev tools at the bottom) then I can see that a correct (my) message is there...

Furthermore in profiler->form errors is this incorect message on the list (as only one item) but... it says it refers to a field named "plainPassword" which is not in the form, and does NOT belong to the "registration" group, so it should not be validated in the first place.

What's going on there?

  • 写回答

1条回答 默认 最新

  • dongya6997 2016-01-08 09:48
    关注

    Can You try this?

    /**
    * @ORM\Column(type="string", length=128, unique=true)
    *
    * 
    * @Assert\NotBlank(
    *  message = "The box cant be left blank")
    *
    *
    * @Assert\Email(
    *  message = "The email '{{ value }}' is not a valid email."
    * 
    * )
    */
    private $email;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)