Php Symfony 2重复字段验证重复消息
我在symfony 2有这张登记表。 密码字段为“重复”类型:Php Symfony 2重复字段验证重复消息,php,forms,validation,symfony,error-handling,Php,Forms,Validation,Symfony,Error Handling,我在symfony 2有这张登记表。 密码字段为“重复”类型: $builder ... ->add('password', 'repeated', array( 'type' => 'password', 'invalid_message' => 'The passwords must match.', 'required' => true,
$builder
...
->add('password', 'repeated', array(
'type' => 'password',
'invalid_message' => 'The passwords must match.',
'required' => true,
'first_options' => array('label' => 'Password:'),
'second_options' => array('label' => 'Repeat Password:'),
'constraints' => array(
new Assert\NotBlank(array('message' => 'Password should not be blank.')),
new Assert\Length(array(
'min' => 8,
'max' => 40,
'minMessage' => "Password must be at least {{ limit }} characters long.",
'maxMessage' => "Password cannot be longer than {{ limit }} characters.",
)),
)
))
...
在twig模板中,仅使用form.password是不够的,因为不会显示错误消息。我必须先使用form.password
{% if not form.vars.valid %}
...
{{ form_errors(form.password.first) }}
...
{% endif %}
结果是:
The password must be at least 8 characters long. <-- The default error message
Password must be at least 8 characters long. <-- Mine
The password must be at least 8 characters long.
This value is too short. It should have 8 characters or more. <-- Default minMessage message
密码长度必须至少为8个字符 虽然已经晚了,但我仍然没有看到关于同一个问题的太多信息。在symfony 4.4中,这对我很有用
$form->add('password', RepeatedType::class, [
'type' => PasswordType::class,
'required' => true,
'invalid_message' => 'your message',
'first_options' => ['label' => 'Password'],
'second_options' => ['label' => 'Repeat Password'],
'options' => ['error_bubbling' => true],
//'error_bubbling' => false, default
...
]);
您是否在表单模板中使用form_行
?否,我正在手动呈现它们{form_小部件(form.password.first,{'attr':{'pattern':'.{8,}','title':'Minimum 8 characters long'}}}}{{form widget(form password.second,{'attr':{'pattern':{'pattern':'.{8,}','title':'Minimum 8 characters long'}}}})}
对于错误{{form_errors(form.password.first)}
基本上,密码字段给我的错误是:[“password”]=>array(1){[“first”]=>array(2){[0]=>string(48)“密码长度必须至少为8个字符。”[1]=>string(44)“密码长度必须至少为8个字符。”}
@Carondimonio你解决了这个问题吗?我也有同样的问题……同样,这很糟糕,仍然没有解决方法!