当两个密码字段不匹配时,我需要显示一个错误。我试图通过在repeated
password
字段中设置invalid_message
来实现这一点,但当我试图将错误调用为{{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())); }
非常感谢您对此事的投入,先生们:)非常感谢:)
不需要冒泡错误。下面是一个工作示例:
$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) }}