Forms 表格';t显示无效的\u消息错误

Forms 表格';t显示无效的\u消息错误,forms,validation,symfony,error-handling,password-confirmation,Forms,Validation,Symfony,Error Handling,Password Confirmation,当我试图呈现与重复密码字段相关联的错误消息时,遇到了一个问题。问题在于,如果我按照以下方式实现细枝,则不会呈现无效的\u消息 {{ form_start(form, {'attr': {'class': 'form-horizontal', 'role': 'form', 'novalidate': 'novalidate'}}) }} <div class="form-group {% if form.password.vars.errors|length &g

当我试图
呈现与
重复密码字段相关联的错误消息时,遇到了一个问题。问题在于,如果我按照以下方式实现细枝,则不会呈现
无效的\u消息

     {{ form_start(form, {'attr': {'class': 'form-horizontal', 'role': 'form', 'novalidate': 'novalidate'}}) }}
        <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, "Password") }}
          <div class="col-sm-8">
            {{ form_widget(form.password.first) }}   
            <span class="help-block">{{ form_errors(form.password) }}</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.second, "Confirm password") }}
          <div class="col-sm-8">
            {{ form_row(form.password.second) }}

          </div>
        </div>
        ........
错误显示在密码文本框的顶部(这不是我放置错误的区域,因为
span
实际上位于文本框的下方),也没有突出显示文本框。我尽力解决这个问题,但没有成功。如果各位专家能够为解决此问题提供任何专家指导,我将不胜感激

仅供参考:表单控制器上的重复密码

$builder->add( 'password', 'repeated', array( 'type' => 'password', 
                                      'required' => true,
                                      'invalid_message' => ErrorMessages::PASSWORDS_DONOT_MATCH,
                                      'options' => array('attr' => array('class' => 'password-field form-control')),                                                                                   
                                      'first_options'  => array('label' => false,                                                                    
                                                                'label_attr'=>array('class'=>'col-sm-3 control-label')),
                                      'second_options' => array('label' => false,                                                                    
                                                                'label_attr'=>array('class'=>'col-sm-3 control-label')))); 
$builder->add( 'password', 'repeated', array( 'type' => 'password', 
                                      'required' => true,
                                      'invalid_message' => ErrorMessages::PASSWORDS_DONOT_MATCH,
                                      'options' => array('attr' => array('class' => 'password-field form-control')),                                                                                   
                                      'first_options'  => array('label' => false, 
                                                                //here I enable error bubbling so that on the twig it will render the error under the first password text field 
                                                                'error_bubbling' => true,
                                                                'label_attr'=>array('class'=>'col-sm-3 control-label')),
                                      'second_options' => array('label' => false,                                                                    
                                                                'label_attr'=>array('class'=>'col-sm-3 control-label'))));   

解决方案很简单,我会发布这篇文章,以防将来有人遇到这个问题。解决方案是使用
forms_行
呈现字段,例如:
{{form_行(form.password.first)}
,并将
'error\u bubbling'=>true
包含到要显示错误的字段中。包括这一点解决了字段突出显示和错误消息放置的问题,在我的例子中,我在
第一个密码文本框下显示了错误

工作示例

  • 小树枝

     {{ form_start(form, {'attr': {'class': 'form-horizontal', 'role': 'form', 'novalidate': 'novalidate'}}) }}
        <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, "Password") }}
          <div class="col-sm-8">
            {{ form_row(form.password.first) }}   
            <span class="help-block">{{ form_errors(form.password) }}</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.second, "Confirm password") }}
          <div class="col-sm-8">
            {{ form_row(form.password.second) }}
    
          </div>
        </div>
        ........
    
$builder->add( 'password', 'repeated', array( 'type' => 'password', 
                                      'required' => true,
                                      'invalid_message' => ErrorMessages::PASSWORDS_DONOT_MATCH,
                                      'options' => array('attr' => array('class' => 'password-field form-control')),                                                                                   
                                      'first_options'  => array('label' => false, 
                                                                //here I enable error bubbling so that on the twig it will render the error under the first password text field 
                                                                'error_bubbling' => true,
                                                                'label_attr'=>array('class'=>'col-sm-3 control-label')),
                                      'second_options' => array('label' => false,                                                                    
                                                                'label_attr'=>array('class'=>'col-sm-3 control-label'))));