Search code examples
phpsymfonyfosuserbundlephp-5.6symfony-2.8

Add constraint on resetting password


I want to add constraint on password on my Symfony2.8 project. The first constraint I'm trying to create is a minimum length for password.

At the moment I've only implemented the reseting password functionnality with the mail for lost password.

So what I did is create a validation.xml file in MyUsersBundle/Resources/config

<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 Constraint
 !-->
    <class name="FOS\UserBundle\Form\Model\ChangePassword">
        <property name="plainPassword">
<constraint name="Length">
                <option name="min">8</option>
                <option name="minMessage">fos_user.new_password.short</option>
                <option name="groups">
                    <value>MyChangePassword</value>
                </option>
            </constraint>
        </property>
    </class>
</constraint-mapping>

And to make it active I modified config.yml and added

fos_user:
    ...
    change_password:
            form:
                validation_groups: [MyChangePassword, Default]

However when I try to change my password via the resetting link, even if it doesn't respect the constraint, it changes my Password.

Do you know what I've made wrong? In config.yml I also tried to do the same with resetting instead of change_password.


Solution

  • I finally found what where the problems.

    First in config.yml the right line was resetting and not change_password as I'm using the resetting password functionality.

    #var/www/MyProject/app/config/config.yml
    fos_user:
        ...
        resetting:
                form:
                    validation_groups: [MyChangePassword, Default]
    

    The other error was on the class name in MyUsersBundle/Resources/config/validation.xml. I didn't need to useFOS\UserBundle\Model\User instead of FOS\UserBundle\Form\Model\ChangePassword.

    #/MyProject/src/MyUsersBundle/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 Constraint
     !-->
        <class name="FOS\UserBundle\Model\User">
            <property name="plainPassword">
    <constraint name="Length">
                    <option name="min">8</option>
                    <option name="minMessage">fos_user.new_password.short</option>
                    <option name="groups">
                        <value>MyChangePassword</value>
                    </option>
                </constraint>
            </property>
        </class>
    </constraint-mapping>