FOSUserBundle icon indicating copy to clipboard operation
FOSUserBundle copied to clipboard

Add a chapter about extending and overwriting the validation

Open stof opened this issue 14 years ago • 6 comments

The chapter should describe how to add its own rules by keeping the bundle ones and also how to validate without the bundle rules.

stof avatar Aug 18 '11 21:08 stof

So, could you explain how can I do it? :)

kusmierz avatar Feb 08 '12 18:02 kusmierz

i have my own register form type. now i won't remove the password notblank constraint when the user token is a special class in my case a oauth class. So i have get it work with:

  1. override the service definition and remove the validation group.
        <service id="fos_user.registration.form" factory-method="createNamed" factory-service="form.factory" class="Symfony\Component\Form\Form">
            <argument>%fos_user.registration.form.type%</argument>
            <argument>%fos_user.registration.form.name%</argument>
            <argument />
            <argument type="collection">
            </argument>
        </service>
  1. use the getDefaultOptions method to switch to the Profile or another validation_group
<?php
    public function getDefaultOptions(array $options)
    {
        $options = parent::getDefaultOptions($options);

        $tokenUser = $this->context->getToken()->getUser();
        if ($tokenUser instanceof $this->oauthUserClass) {
            $options['validation_groups'] = array('Profile');
        }

        return $options;
    }

Questions: Why is the validation group name not set in the form types getDefaultOptions method?

gimler avatar Feb 20 '12 10:02 gimler

+1

verdet23 avatar Nov 20 '12 14:11 verdet23

I need this too :+1:

stfalcon avatar Nov 20 '12 14:11 stfalcon

:+1:

webdevilopers avatar Sep 22 '14 12:09 webdevilopers

Maybe it's too late, but would it be possible for the 2.x version to move the default constraints group to a prefixed name? For example, the Registration group could be renamed FOS_Registration. This is because there is currently no way for Symfony to override the constraints in a child class, and the current group names imho are too common and general to be used by the bundle instead of the application itself.

ste93cry avatar Feb 13 '17 10:02 ste93cry