0

当两个密码字段不匹配时,我需要显示错误。我试图通过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()));
      }  
      

先生们非常感谢您对此事的意见:)非常感谢您:)

4

1 回答 1

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) }}
于 2014-10-06T14:08:13.690 回答