如果您的表單基於實體,則應使用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',你只能使用'消息'。