仅当新密码长度为1个字符时,密码验证消息才会重复 [英] Duplicated password validation messages ONLY IF new password is 1 char long
问题描述
我的问题是密码验证消息在注册和更改密码"表单中出现两次.正如大多数帖子/解决方案指出的组"一样,如何在下面的代码中实现它?我尝试实现如下所示的其他已解决示例,但无法使其正常工作.也许是因为我以前从未使用过FOSUserBundle.
My problem is password validation message appears twice in Registration and Change Password forms. As most posts/solutions points out "groups", how do I implement it into my code below? I tried to implement other solved examples as shown below but I cannot get it working. Maybe because I never worked FOSUserBundle before.
- FOSUserBundle-验证用户名,密码或电子邮件 字段
- 使用多个验证时约束重复的错误 组
- Symfony2 FOSUserBundle扩展注册表单会导致重复 电子邮件进行验证
- 验证fosuserbundle注册表格
- 表单验证-我正在标签两次
- FOSUserBundle - Validation for username, password or email fields
- Duplicated errors for constraints when using several validation groups
- Symfony2 FOSUserBundle extending registration form causes duplicate email to validate
- validating fosuserbundle registration form
- Validation of a form - I'm getting the labels two times
myapp/app/config/config.yml
fos_user:
db_driver: orm
firewall_name: main
user_class: WebsiteBundle\Entity\User
model_manager_name: websitemanager
registration:
form:
type: website_user_registration
change_password:
form:
type: fos_user_change_password
validation_groups: [ChangePassword, Default]
网站捆绑/资源/translations/validators.en.yml
fos_user:
password:
short: "[-Inf,Inf]The password must contain at least 8 characters"
services.xml
<service id="myapp_website.registration.form.type"
class="myapp\WebsiteBundle\Form\Type\RegistrationFormType">
<tag name="form.type" alias="website_user_registration" />
<argument>myapp\WebsiteBundle\Entity\User</argument>
</service>
WebsiteBundle/Form/Type/RegistrationFormType.php
namespace myapp\WebsiteBundle\Form\Type;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
use Symfony\Component\Validator\Constraints\IsTrue;
class RegistrationFormType extends BaseType
{
public function __construct($class)
{
parent::__construct($class);
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder->add(
'terms',
'checkbox',
[
'label' => 'Older than 18',
'constraints' => [
new IsTrue([
'message' => 'Are you older than 18?',
]),
],
'required' => true,
'mapped' => false,
]
);
}
public function getName()
{
return 'website_user_registration';
}
}
validation.xml
<?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="FOS\UserBundle\Model\User">
<property name="plainPassword">
<constraint name="NotBlank">
<option name="message">fos_user.password.blank</option>
<option name="groups">
<value>Registration</value>
<value>ResetPassword</value>
<value>ChangePassword</value>
</option>
</constraint>
<constraint name="Length">
<option name="min">8</option>
<option name="minMessage">fos_user.password.short</option>
<option name="groups">
<value>Registration</value>
<value>Profile</value>
<value>ResetPassword</value>
<value>ChangePassword</value>
</option>
</constraint>
</property>
<property name="email">
<constraint name="NotBlank">
<option name="message">Please enter your email address</option>
<option name="groups">
<value>Registration</value>
<value>Profile</value>
</option>
</constraint>
</property>
<property name="username">
<constraint name="NotBlank">
<option name="message">Please enter your name</option>
<option name="groups">
<value>Registration</value>
<value>Profile</value>
</option>
</constraint>
</property>
</class>
</constraint-mapping>
嫩枝
{% trans_default_domain 'FOSUserBundle' %}
<form action="{{ path('fos_user_change_password') }}" {{ form_enctype(form) }} method="POST">
{{ form_row(form.current_password, {'label': 'Current Password'}) }}
{{ form_row(form.plainPassword.first, {'label': 'New Password'}) }}
{{ form_row(form.plainPassword.second, {'label': 'Confirm Password'}) }}
{{ form_rest(form) }}
<br /><input id="submit" type="submit" value="Change password" />
</form>
HTML结果
<label for="fos_user_registration_form_plainPassword_first">Password</label>
<input type="password" id="fos_user_registration_form_plainPassword_first" name="fos_user_registration_form[plainPassword][first]" required="required" />
<label for="fos_user_registration_form_plainPassword_second">Confirm password</label>
<input type="password" id="fos_user_registration_form_plainPassword_second" name="fos_user_registration_form[plainPassword][second]" required="required" />
错误
当我在代码库中有validators.en.yml
时.
Errors
When I have validators.en.yml
in code-base.
当我从代码库中删除validators.en.yml
时.
When I remove validators.en.yml
from code-base.
推荐答案
看看group_sequence功能以进行验证.
Have a look at the group_sequence feature for validations.
# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\User:
group_sequence:
- User
- Strict
getters:
passwordLegal:
- 'IsTrue':
message: 'The password cannot match your username'
groups: [Strict]
properties:
username:
- NotBlank: ~
password:
- NotBlank: ~
在此示例中,它将首先验证组中的所有约束 用户(与默认"组相同).仅在所有约束条件下 在该组中有效,第二组Strict将被验证.
In this example, it will first validate all constraints in the group User (which is the same as the Default group). Only if all constraints in that group are valid, the second group, Strict, will be validated.
这直接取自Symfony文档,网址为 http://symfony .com/doc/current/book/validation.html#group-sequence
This is taken straight of the Symfony documentation at http://symfony.com/doc/current/book/validation.html#group-sequence
但是我个人无法使用此功能.
However personally I could never get this feature to work.
这篇关于仅当新密码长度为1个字符时,密码验证消息才会重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!