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条)

报告相同问题?