89767

Duplicated password validation messages ONLY IF new password is 1 char long

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.

    <li>FOSUserBundle - Validation for username, password or email fields</li> <li>Duplicated errors for constraints when using several validation groups</li> <li>Symfony2 FOSUserBundle extending registration form causes duplicate email to validate</li> <li>validating fosuserbundle registration form</li> <li>Validation of a form - I'm getting the labels two times</li> </ul>

    <strong>myapp/app/config/config.yml</strong>

    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]

    <strong>WebsiteBundle/Resources/translations/validators.en.yml</strong>

    fos_user: password: short: "[-Inf,Inf]The password must contain at least 8 characters"

    <strong>services.xml</strong>

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

    <strong>WebsiteBundle/Form/Type/RegistrationFormType.php</strong>

    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'; } }

    <strong>validation.xml</strong>

    <?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>

    <strong>Twig</strong>

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

    <strong>HTML result</strong>

    <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" />

    <strong>Errors</strong> When I have validators.en.yml in code-base.

    <img src="https://cloud.githubusercontent.com/assets/8837278/12117475/e3fc7e0c-b3b8-11e5-9f2f-7eddc8effe7e.jpeg" alt="8-char">

    When I remove validators.en.yml from code-base.

    <img src="https://cloud.githubusercontent.com/assets/8837278/12117489/fbcd8c4c-b3b8-11e5-8cd8-71054b0bfbb2.png" alt="screenshot from 2016-01-05 14 28 29">

    Answer1:

    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: ~

    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.

    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.

Recommend

  • JENKINS: ERROR when I try to use an older JDK for a specific maven project
  • HttpServletRequest getPart(\") not found
  • Failed to read schema document 'http://www.springframework.org/schema/tool/spring-tool-4.1.xsd&
  • custom string delimiters stringtemplate-4
  • How do I refer to a client_deploy.wsdd file that's in WEB-INF?
  • Request response issues in biztalk
  • Validate jQuery plugin, field not required
  • How can I include If-None-Match header in HttpRequestMessage
  • cordova is not defined - cordova.js has already been loaded :: Ionic
  • Clear fused location provider's location for testing
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • How do I signal completion of my dataflow?
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • Parse a date string in a specific locale (not timezone!)
  • Parsing a CSV string while ignoring commas inside the individual columns
  • azure media services - The request body is too large and exceeds the maximum permissible limit
  • Play WS (2.2.1): post/put large request
  • Avoid links criss cross / overlap in d3.js using force layout
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deserializing XML into class C#
  • When should I choose bucket sort over other sorting algorithms?
  • Websockets service method fails during R startup
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Do I've to free mysql result after storing it?
  • How to get next/previous record number?
  • Apache 2.4 - remove | delete | uninstall
  • Is there a mandatory requirement to switch app.yaml?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Proper way to use connect-multiparty with express.js?
  • Trying to get generic when generic is not available
  • embed rChart in Markdown
  • How to stop GridView from loading again when I press back button?
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal