My problem is that the password verification message appears twice in the Password Registration and Change forms. Since most posts / solutions point to βgroups,β how do I implement it in my code below? I tried to implement other permitted examples as shown below, but I cannot get it to work. Perhaps because I did not work with FOSUserBundle before.
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]
WebsiteBundle / Resources / 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>
Twig
{% 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 result
<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" />
Errors When I have validators.en.yml in the code base.

When I validators.en.yml from the code base.
