dshmkgq558192365 2015-08-09 10:02
浏览 39
已采纳

Symfony 2-验证消息

I use symfony validator assert, in my class subscription i have:

/**
 * @var string
 * @Assert\NotBlank(message="asasass")
 * */
    private $name;

Forms:

class SubscriptionType extends AbstractType{

    public function getName() {
        return 'Piotrek_subscription';
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
                ->add('name', 'text', array(
                    'label' => 'Imię i nazwisko'

                ))
                ->add('email', 'text', array(

                ))
                ->add('save', 'submit', array(
                    'label' => 'Zapisz się'
                ));        }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Piotrek\WitajBundle\Entity\Subscription'
        ));
    }
    }

And my controler:

$Subscription = new Subscription();

        $form = $this->createForm(new SubscriptionType(), $Subscription);


        return array(
            'form' => $form->createView(),

My form don't read the custom validate in comment. Imposes standard , but also do not know where. When I delete comment the ruler is still the same. So how do you change the text validation?

  • 写回答

2条回答 默认 最新

  • douwen5951 2015-08-09 11:40
    关注

    You constraint is a server side one, that means when you submit your form the page reloads and you will get the errors. But the message you see is the default HTML5 error message when you try to submit an empty field, because unless you mention otherwise inside your formbuiler, all fields will be rendered as required that's why you get the default hhtml 5 error message

    You can do what want using:

    Method 1: this will display your custom message "asasasas" after loading the page

    set the required option as false

    builder->add('name', 'text',array('required' => false )
    

    Method 2:

    Change the default htm5 error message from inside your SubscriptionType , something like this: ( I don't remember it exactly but this could point you to the right path)

    builder->add('name' ,'text',array(
    'attr'=>array('oninvalid'=>"setCustomValidity('bla bla.. ')")
    

    Hope this works for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)