Symfony validate form with mapped false form fields

后端 未结 5 1062
野的像风
野的像风 2020-12-07 11:47

I\'ve got a form with extra fields added with the option mapped to false. But when I try to validate my form, it won\'t pass indicating \"this valu

5条回答
  •  既然无缘
    2020-12-07 12:16

    As I mentionned in a question on a similar topic, since Symfony 2.1, you should use the 'constraints' option to add validation to your un-mapped fields:

    use Symfony\Component\Validator\Constraints\MinLength;
    use Symfony\Component\Validator\Constraints\NotBlank;
    
    $builder
        ->add('firstName', 'text', array(
            'constraints' => new MinLength(3),
        ))
        ->add('lastName', 'text', array(
            'constraints' => array(
                new NotBlank(),
                new MinLength(3),
            ),
        ))
    ;
    

    Hope it will help anybody like me who lost some time on this...

提交回复
热议问题