如果您的表单基于实体,则应使用validation.yml文件来使用约束。如果不是(或者,如果你是懒惰的),你也可以使用PHP添加约束您的形式是这样的:
#Load the constraint you are going to use (for example length, which put min or max limits to your input length
use Symfony\Component\Validator\Constraints\Length;
# ...
#then in your controller or formtype where you build your form:
$form=$this->createFormBuilder()
->add('newPassword', 'password', array(
'required' => true,
'constraints' => array(
new Length(array(
'min' => 8,
'minMessage' => 'You have entered {{ value }} which is under the limit length {{ limit }}')))))
->getForm();
在这个例子中,我建立这必须是上面的限制密码输入(8)。请注意我在此输入中如何实现length
约束。然后使用{{ value }}
打印该消息以访问输入的值。你应该找到不同的约束如何实现消息,在'长度约束'的情况下,你可以为其他约束设置'minMessage'和'maxMessage',你只能使用'消息'。