form_widget不显示invalid_message错误

时间:2014-10-07 04:34:28

标签: forms validation symfony error-handling password-confirmation

当我尝试render an error messagerepeated password field相关联时,我遇到了一个问题。问题是,如果我按如下方式实现树枝,则invalid_message无法渲染,

     {{ 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>
        ........

但是,如果我使用form_row

     {{ 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>
        ........

错误显示在密码文本框的顶部(这不是我放置错误的区域,因为span实际上位于文本框下方)并且没有突出显示文本框。我尽力解决这个问题,但没有运气。如果您的专家可以提供任何专家指导来解决此问题,我将非常感激。 :)

FYI :表单控制器上的重复密码

$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')))); 

1 个答案:

答案 0 :(得分:2)

解决方案很简单,并且发布此信息以防将来遇到此问题。解决方案是使用forms_row来渲染字段,例如:{{ form_row(form.password.first) }}并将'error_bubbling' => true包含在您希望显示错误的字段中。包含这个解决了字段突出显示和错误消息展示位置的问题,在我的情况下,我在first password text box下显示了错误。

工作示例,

  • twig

     {{ 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>
        ........
    
  • form控制器

    $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'))));