本文介绍了验证表单 - 我得到两次标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! {%form_theme form'AcmeMyBundle:Form: errors.html.twig'%} < form class =big-spaceraction ={{path('fos_user_registration_register')}}{{form_enctype(form)}} method = POSTclass =fos_user_registration_register> {{form_widget(form)}} < div> < input class =btn little-spacertype =submitvalue ={{'registration.submit'| trans({},'FOSUserBundle')}}/> < / div> < / form> 以下是 errors.html.twig : {%block field_errors%} {%spaceless%} {%if错误|长度> 0%} < ul class =little-spacer nav text-error> {%为错误中的错误%} < li> {{loop.index}}。 {{error.messageTemplate | trans(error.messageParameters,'validators')}}< / li> {%endfor%} < / ul> {%endif%} {%endspaceless%} {%endblock field_errors%} 我刚刚从TwitterBootstrap中添加了一些css类。 问题是我得到了一些验证信息两次。 我的表单有4个字段 - 用户名,电子邮件,密码,确认密码 对于用户名:我尝试打破尽可能多的验证规则, / p> 此用户名已被使用。请选择另一个。 用户名太短 - 请输入至少2个符号。 用户名太短 - 请至少输入2个符号。 对于电子邮件: 请输入一个有效的电子邮件。 请输入一个有效的电子邮件地址。 ,如果我输入已经使用过的电子邮件,错误只显示一次: 对于密码: 如果它们很短: 密码太短 - 请输入至少6个符号 密码太短 - 请输入至少6个符号。 't match: 输入的密码不匹配。 另一个奇怪的是,当我重新提交表单,但仍然无效时,通知密码的长度只有一个: 密码太短 - 请输入至少6个符号。 在重新提交之前,它们是两个。 你有什么想法,为什么一些错误显示两次,以及如何解决这个问题?非常感谢您提前! :) 更新 这是 C:\xampp\htdocs\Project\src\Acme\MyBundle\Entity\User.php 命名空间Acme\MyBundle\Entity; 使用FOS \UserBundle\Entity\User作为BaseUser; 使用Doctrine \ORM\Mapping作为ORM; $ b / ** * @ ORM\Entity * @ ORM\Table(name =fos_user) * / class User扩展BaseUser { / ** * @ ORM\Id * @ ORM\Column(type =integer) * @ ORM \ GeneratedValue(strategy =AUTO) * / 保护$ id; public function __construct() { parent :: __ construct(); //你自己的逻辑} } C:\xampp\htdocs\Project\app\Resources\FOSUserBundle\translations\ 我复制了文件验证器。 en.yml 并在其中删除 [ - Inf,Inf] 部分并稍微更改了消息。 我也覆盖了验证文件 - 我在这里复制它: C: \xampp\htdocs\Project\src\Acme\MyBundle\Resources\config\validation.xml 我只更改了密码的最小长度。其他所有内容与原始文件中的一样。 我的包扩展了FOSUserBundle: C:\xampp\htdocs\Project\src\Acme\MyBundle\AcmeMyBundle.php 此文件包含以下内容: <?php 命名空间Acme\BudgetTrackerBundle; 使用Symfony \ Component \HttpKernel\Bundle\Bundle; class AcmeBudgetTrackerBundle扩展Bundle { public function getParent() { return'FOSUserBundle'; } } 解决方案好的,这是一个已知的问题。 让我们看看这里: https://github.com/symfony/symfony/issues/2605 解决方案是:为您想要的属性的验证规则创建您自己的验证组重写。在您的validation.xml文件中,只放入您想要一些不同规则的属性,并在新的验证组上定义验证。因此,对于validation.xml,只能修改plainPassword验证规则(Acme \ MyBundle \Entity\User): <?xml version =1.0 ?> < constraint-mapping xmlns =http://symfony.com/schema/dic/constraint-mapping xmlns:xsi =http://www.w3.org/2001/XMLSchema -instance xsi:schemaLocation =http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping -1.0.xsd> < class name =Acme\MyBundle\Entity\User> < property name =plainPassword> < constraint name =NotBlank> < option name =message> fos_user.password.blank< / option> < option name =groups>注册< / option> < / constraint> < constraint name =长度> < option name =min> 6< / option> < option name =minMessage> fos_user.password.short< / option> < option name =groups> <值> RegistrationAcme< /值> <值> ProfileAcme< /值> < / option> < / constraint> < / property> < / class> < / constraint-mapping> 现在您必须声明您对受影响的表单(注册和配置文件)使用了不同的验证组, 。幸运的是,FOSUserBundle是一个良好实践的例子,并允许您在config.yml中覆盖它们: fos_user:注册:表单: validation_groups:[Default,RegistrationAcme] 配置文件:表单: validation_groups:[默认,ProfileAcme] I have a registration form - from FOSUserBundle this is in the template:{% form_theme form 'AcmeMyBundle:Form:errors.html.twig' %} <form class="big-spacer" action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register"> {{ form_widget(form) }} <div> <input class="btn little-spacer" type="submit" value="{{ 'registration.submit'|trans({}, 'FOSUserBundle') }}" /> </div> </form>Here is errors.html.twig:{% block field_errors %} {% spaceless %} {% if errors|length > 0 %} <ul class="little-spacer nav text-error"> {% for error in errors %} <li>{{loop.index}}. {{ error.messageTemplate|trans(error.messageParameters, 'validators') }}</li> {% endfor %} </ul> {% endif %} {% endspaceless %}{% endblock field_errors %}I have just added some css classes from TwitterBootstrap.The problem is that I get some of the messages for the validation twice.My form has 4 fields - Username, Email, Password, Confirm PasswordI tried to break as many validation rules I can and here is the output:For Username:This username is already used. Please choose another one.The username is too short - please enter at least 2 symbols.The username is too short - please enter at least 2 symbols.For email:Please enter a valid email.Please enter a valid email.and if I enter already used email, the error is shown only once:This email is already used.And for the passwords:If they are short:The password is too short - please enter at least 6 symbols.The password is too short - please enter at least 6 symbols.And if they don't match:The entered passwords don't match.Another strange thing is that when I resubmit the form, but it's still not valid, the notice for the length of the password is only one:The password is too short - please enter at least 6 symbols.and before resubmitting, they were two.Do you have any ideas why some of the errors are displayed twice and how to fix this? Thank you very much in advance! :)UPDATEThis is C:\xampp\htdocs\Project\src\Acme\MyBundle\Entity\User.phpnamespace Acme\MyBundle\Entity;use FOS\UserBundle\Entity\User as BaseUser;use Doctrine\ORM\Mapping as ORM;/** * @ORM\Entity * @ORM\Table(name="fos_user") */class User extends BaseUser{ /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; public function __construct() { parent::__construct(); // your own logic }}And in C:\xampp\htdocs\Project\app\Resources\FOSUserBundle\translations\ I copied the file validators.en.yml and in it and removed the [-Inf, Inf] part and changed the messages a bit.I also overrode the validation file - I copied it here:C:\xampp\htdocs\Project\src\Acme\MyBundle\Resources\config\validation.xmlI changed only the minimum length of the password. Everything else is the same as in the original file.My bundle extends FOSUserBundle:C:\xampp\htdocs\Project\src\Acme\MyBundle\AcmeMyBundle.php this file contains the following:<?phpnamespace Acme\BudgetTrackerBundle;use Symfony\Component\HttpKernel\Bundle\Bundle;class AcmeBudgetTrackerBundle extends Bundle{ public function getParent() { return 'FOSUserBundle'; }} 解决方案 Ok, this is a known issue.Let's have a look here : https://github.com/symfony/symfony/issues/2605The solution is : create your own validation group for properties' validation rules you want to override. In your validation.xml, put only properties you want some different rules, and define validation on a new validation group.So, for validation.xml, to modify only plainPassword validation rules for your entity (Acme\MyBundle\Entity\User) :<?xml version="1.0" ?><constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mappinghttp://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> <class name="Acme\MyBundle\Entity\User"> <property name="plainPassword"> <constraint name="NotBlank"> <option name="message">fos_user.password.blank</option> <option name="groups">Registration</option> </constraint> <constraint name="Length"> <option name="min">6</option> <option name="minMessage">fos_user.password.short</option> <option name="groups"> <value>RegistrationAcme</value> <value>ProfileAcme</value> </option> </constraint> </property> </class></constraint-mapping>And now you have to state that you use different validation groups for the impacted forms (registration and profile). Fortunately, FOSUserBundle is a good practices' example and allows you to override them in your config.yml :fos_user: registration: form: validation_groups: [Default, RegistrationAcme] profile: form: validation_groups: [Default, ProfileAcme] 这篇关于验证表单 - 我得到两次标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-06 04:45