当两个密码字段不匹配时,我需要显示错误。我试图通过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())); }
先生们非常感谢您对此事的意见:)非常感谢您:)