Php 重复字段上的字段特定错误

Php 重复字段上的字段特定错误,php,forms,validation,symfony,error-handling,Php,Forms,Validation,Symfony,Error Handling,我需要根据情况在特定字段下显示错误。就我而言,可能有两个。密码为空和密码不匹配。当前所有消息都显示在password.second字段下 我需要在密码下显示密码为空错误。第一个字段和密码下的密码不匹配。第二个字段 我试图分别通过{%for error in form.password.first.vars.errors%}和{%for error in form.password.second.vars.errors%}生成错误,但失败了,因为只有在使用{%for error in form.p

我需要根据情况在特定字段下显示错误。就我而言,可能有两个。密码为空和密码不匹配。当前所有消息都显示在
password.second
字段下

我需要在
密码下显示
密码为空
错误。第一个
字段和
密码下的
密码不匹配。第二个
字段

我试图分别通过
{%for error in form.password.first.vars.errors%}
{%for error in form.password.second.vars.errors%}
生成错误,但失败了,因为只有在使用
{%for error in form.password.vars.errors%}
时,我才需要两条错误消息。我已经做了很长一段时间了,回来时两手空空。具体实施情况如下:

$builder->add( 'password', 'repeated', array( 'type' => 'password','required' => true,  'invalid_message' => ErrorMessages::PASSWORDS_DO_NOT_MATCH, 'options' => array('attr' => array('class' => 'password-field form-control')),'first_options'  => array('label' => false,'error_bubbling' => true,'label_attr'=>array('class'=>'col-sm-4 control-label')),'second_options' => array('label' => false,'label_attr'=>array('class'=>'col-sm-4 control-label')))); 
class User implements UserInterface, \Serializable {    

  /**
   * @Assert\Email(message=ErrorMessages::EMAIL_ADDRESS_INVALID)
   * @Assert\NotBlank(message=ErrorMessages::EMAIL_ADDRESS_EMPTY)
   */
  private $email;

  /**     
   * @Assert\NotBlank(message=ErrorMessages::PASSWORD_EMPTY, groups={"full"})
   */
  private $password;
  ....
我正在使用
repeated
字段来实现
密码
确认密码
,如下所示

$builder->add( 'password', 'repeated', array( 'type' => 'password','required' => true,  'invalid_message' => ErrorMessages::PASSWORDS_DO_NOT_MATCH, 'options' => array('attr' => array('class' => 'password-field form-control')),'first_options'  => array('label' => false,'error_bubbling' => true,'label_attr'=>array('class'=>'col-sm-4 control-label')),'second_options' => array('label' => false,'label_attr'=>array('class'=>'col-sm-4 control-label')))); 
class User implements UserInterface, \Serializable {    

  /**
   * @Assert\Email(message=ErrorMessages::EMAIL_ADDRESS_INVALID)
   * @Assert\NotBlank(message=ErrorMessages::EMAIL_ADDRESS_EMPTY)
   */
  private $email;

  /**     
   * @Assert\NotBlank(message=ErrorMessages::PASSWORD_EMPTY, groups={"full"})
   */
  private $password;
  ....
密码实体的验证如下所示:

$builder->add( 'password', 'repeated', array( 'type' => 'password','required' => true,  'invalid_message' => ErrorMessages::PASSWORDS_DO_NOT_MATCH, 'options' => array('attr' => array('class' => 'password-field form-control')),'first_options'  => array('label' => false,'error_bubbling' => true,'label_attr'=>array('class'=>'col-sm-4 control-label')),'second_options' => array('label' => false,'label_attr'=>array('class'=>'col-sm-4 control-label')))); 
class User implements UserInterface, \Serializable {    

  /**
   * @Assert\Email(message=ErrorMessages::EMAIL_ADDRESS_INVALID)
   * @Assert\NotBlank(message=ErrorMessages::EMAIL_ADDRESS_EMPTY)
   */
  private $email;

  /**     
   * @Assert\NotBlank(message=ErrorMessages::PASSWORD_EMPTY, groups={"full"})
   */
  private $password;
  ....
我以以下方式在
细枝中呈现这两个字段:

<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) }}                      
  </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) }}
    <span class="help-block">
      {% for error in form.password.vars.errors %}
        {{ error.messageTemplate|trans(error.messageParameters, 'validators')~'' }}
      {% endfor %}
    </span>
  </div>
</div>

{{form_标签(form.password.first,“password”)}
{{form_行(form.password.first)}
{{form_标签(form.password.second,“确认密码”)}
{{form_行(form.password.second)}
{%form.password.vars.errors%}
{{error.messageTemplate | trans(error.messageParameters,'validators')~''}
{%endfor%}

如果您想使用方便的
repeated
字段,那么您可能会被卡住-重复
的工作方式是使用一个值来复制,即将添加到包含相同数据的两个字段中的字段复制,并且它确保在转换器将提交的表单值转换回模型数据时(不使用常规约束),它们是相同的。从验证的PoV来看,只有一个字段,因此您无法选择错误所针对的文本框

但如果你想自己解决问题,可能还有其他选择

你可以做出一个决定。要实现您所追求的行为,我认为您需要它是一个,以便它可以同时访问这两个字段,并可以比较它们。然而,这将再次意味着错误将被应用到比您想要的更高的级别,因此可能无法工作

因此,可能的方法是手动测试控制器中的字段,并将相关错误直接添加到要关联的字段中,如下所示。您需要将密码字段一分为二,以便可以相互测试,因此在第一个字段上设置
NotBlank
约束

控制器


您使用的是FOSUserBundle,还是这都是原创作品?只是想知道当前行为来自何处。啊,对不起,您自己添加了
repeated
字段的地方没有找到@非常感谢你的答复。。是的,我使用的是重复字段:)谢谢你的描述性回答。我将尝试一下并发回:)