当两个密码字段不匹配时,我需要显示错误。我试图通过在invalid_message
repeated
字段中设置password
来实现此目的,但当我尝试将错误称为{{form_errors(form.password)}}
时,错误不会出现。但是,如果我使用{{form_errors(form)}}
,则会出现密码不匹配错误。我需要使错误字段具体,并且会非常重视您对此的输入:)
我尝试了很多在线搜索,但没有人帮忙。以下是我的实施,
twig
{{ form_start(form, {'attr': {'class': 'form-horizontal', 'role': 'form', 'novalidate': 'novalidate'}}) }}
<!--just placed the error here for debug purposes-->
{{ form_errors(form) }}
<div class="form-group {% if form.email.vars.errors|length > 0 %}has-error{% endif %} {% if form.email.vars.required == 'true' %}required{% endif %}">
{{ form_label(form.email) }}
<div class="col-sm-8">
{{ form_widget(form.email) }}
<span class="help-block">{{ form_errors(form.email) }}</span>
</div>
</div>
<div class="form-group {% if form.password.vars.errors|length > 0 %}has-error{% endif %} {% if form.password.vars.required == 'true' %}required{% endif %}">
{{ form_label(form.password.first) }}
<div class="col-sm-8">
{{ form_widget(form.password.first) }}
</div>
</div>
<div class="form-group required">
{{ form_label(form.password.second) }}
<div class="col-sm-8">
{{ form_widget(form.password.second) }}
</div>
</div>
<div class="form-group">
<div class="center-block btn-sign-in">
{{ form_widget(form.submit) }}
</div>
</div>
<span class="form-footer-msg">Already have an account? <a href="/signin">Sign In</a></span>
{{ form_end(form) }}
form
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('email', 'email', array('label'=>'Email',
'attr'=>array('class'=>'form-control'),
'required'=>true,
'trim' => true,
'data'=>'user1@test.com',
'label_attr'=>array('class'=>'col-sm-3 control-label')));
$builder->add( 'password', 'repeated', array( 'type' => 'password',
//here is the password mismatch error message
'invalid_message' => ErrorMessages::PASSWORDS_DONOT_MATCH,
'options' => array('attr' => array('class' => 'password-field form-control')),
'error_bubbling' => true,
'required' => true,
'trim' => true,
'first_options' => array('label' => 'Password',
'error_bubbling' => true,
'label_attr'=>array('class'=>'col-sm-3 control-label')),
'second_options' => array('label' => 'Confirm password',
'error_bubbling' => true,
'label_attr'=>array('class'=>'col-sm-3 control-label'))));
$builder->add('submit', 'submit', array('label'=>'Create Account',
'attr'=>array('class'=>'btn btn-primary')))
->setMethod('POST')
->getForm();
}
public function getName() {
return 'signup';
}
controller
public function indexAction() {
$signUp = new User();
$form = $this->createForm(new SignUpForm(), $signUp,
array('action'=>$this->generateUrl('accounts_signup')));
$request = $this->get('request');
$form->handleRequest($request);
if ($request->getMethod() == 'POST') {
if ($form->isValid()) {
$request = $this->get("request");
//do something
}else{
//do something else
}
}
return $this->render('AccountsBundle:Signup:index.html.twig',
array('form'=>$form->createView()));
}
非常感谢您对这件事的意见。非常感谢你:))
答案 0 :(得分:1)
不需要错误冒泡。这是一个有效的例子:
$builder->add('password', 'repeated', array(
'type' => 'password',
'label' => 'Zayso Password',
'required' => true,
'attr' => array('size' => 20),
'invalid_message' => 'The password fields must match.',
'constraints' => new NotBlankConstraint($constraintOptions),
'first_options' => array('label' => 'Zayso Password'),
'second_options' => array('label' => 'Zayso Password(confirm)'),
'first_name' => 'pass1',
'second_name' => 'pass2',
));
# twig
{{ form_row(form.password.pass1) }}
{{ form_row(form.password.pass2) }}