3
votes

I am using FOSUserBundle. I have implemented my own user entity with additional class variables. Registration, login, and lost password are working as expected. :-)

I need to add additional requirements to the password validation. I found a Symfony article in the Symfony docs. This article explains how to create a validation method and reference it in the validation.xml file with a Callback constraint. I have implemented the callback and changed validation.xml and everything is working just fine for registration.

What I haven't figured out is how to get it working for password changes via the lost password link or Change Password page.

Advice and documentation pointers will be very welcome.

Thanks in advance.

1
You should paste your code and the people could help you better. Thank you. - Airam

1 Answers

7
votes

Okay. Turns out what I needed to do is create a validate.xml file for my user bundle and use group names different from those used by FOSUserBundle. Also needed to update config.yml to specify my groups. Specifically, I modified config.yml thusly:

fos_user:
    ...
    change_password:
        form:
            validation_groups: [myChangePassword]

And in src/myProject/UserBundle/Resources/config/validation.xml:

<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">
  <!--
   ! Password constraints:
   !-->
  <class name="FOS\UserBundle\Form\Model\ChangePassword">
    <property name="new">
      <constraint name="Regex">
        <option name="pattern">/[0-9!@#$%^*_-]+/</option>
        <option name="message">Must include at least one digit or !,@,#,$,%,^,*</option>
        <option name="groups">
          <value>myChangePassword</value>
        </option>
      </constraint>
      <constraint name="Length">
        <option name="min">8</option>
        <option name="max">254</option>
        <option name="minMessage">Must be at least eight characters</option>
        <option name="maxMessage">Too long</option>
        <option name="groups">
          <value>myChangePassword</value>
        </option>
      </constraint>
    </property>
  </class>
</constraint-mapping>

Hope this will save someone else some time.