Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed validation_groups when option is a Closure #93

Closed
wants to merge 1 commit into from

Conversation

eko
Copy link

@eko eko commented Jan 7, 2016

Hi,

I'm having an issue using this bundle on my project when having a validation_groups option which is a Closure.

I have a companyName property on my data class which can be null when using person validation group and cannot be blank when we are using the company validation group following:

    /**
     * @var string
     * @Type("string")
     * @Symfony\Component\Validator\Constraints\NotNull(groups={"company"})
     * @Symfony\Component\Validator\Constraints\Null(groups={"person"})
     */
    protected $companyName;

Here is the closure I use to define the validation_groups option in my form type:

    /**
     * {@inheritdoc}
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'validation_groups' => function (FormInterface $form) {
                return $form->getData()->isCompany() ? array('company') : array('person');
            },
        ));

Here is the output with the current code:

[
    'Symfony\\Component\\Validator\\Constraints\\NotNull':[
        {
            'message':'PinpadFullUserAccountInput.companyName.Null.message',
            'groups':[
                'Default',
                'PinpadFullUserAccountInput',
                'FullUserAccountInput'
            ]
        },
    'Symfony\\Component\\Validator\\Constraints\\Null':[
        {
            'message':'PinpadFullUserAccountInput.companyName.Null.message',
            'groups':[
                'Default',
                'PinpadFullUserAccountInput',
                'FullUserAccountInput'
            ]
        }
]

My form does not validate with the current code as it does not know the validation groups as it retrieves the class names instead.

Using my fix, I got a correct output and my form now validates:

[
    'Symfony\\Component\\Validator\\Constraints\\NotNull':[
        {
            'message':'PinpadFullUserAccountInput.companyName.NotNull.message',
            'groups':[
                'company'
            ]
        },
    'Symfony\\Component\\Validator\\Constraints\\Null':[
        {
            'message':'PinpadFullUserAccountInput.companyName.Null.message',
            'groups':[
                'person'
            ]
        }
]

Note: I've also fixed the satooshi/php-coveralls dependency to retrieve the latest 1.0.x compatible version as 2.0.x will be available for PHP >= 5.5 only and it breaks the the composer installation (using --dev) actually.

Thank you for your quick answer.

@eko
Copy link
Author

eko commented Jan 7, 2016

Hi @66Ton99,

Could you have a look on this fix please?

Thank you

Update composer.json dependencies

Added some unit tests
@eko
Copy link
Author

eko commented Jan 11, 2016

I've also added some unit tests to test the different cases of the getValidationGroups() method.

@66Ton99, @benr77, @manuelj555, @asentner, is there someone for reviewing this please? This is blocking me for a project.

Thank you

@eko
Copy link
Author

eko commented Jan 13, 2016

Hello @66Ton99, @benr77, @manuelj555, @asentner,

Sorry for insisting but I really need an answer on this subject, can you please review this?

Thank you

@66Ton99
Copy link
Collaborator

66Ton99 commented Jan 13, 2016

Hi @eko, sorry for delay.
The code looks good for me, but I am worrying about Travis and it's fails. I need time to fix it or any help with it.

@eko
Copy link
Author

eko commented Jan 13, 2016

Hi @66Ton99,

Thank you for your answer!

Great, you can run the test manually after applying the patch of this PR in order to check that the JsFormValidatorFactory::getValidationGroups() method works well due to unit test I added:

$ phpunit Tests/Factory/JsFormValidatorFactoryTest.php

I don't know why Travis is failing but it was failing before this PR. I do not have so much time to have a look but I will try to do so if I have some time.

Thank you

@66Ton99
Copy link
Collaborator

66Ton99 commented Jan 13, 2016

@eko one more thing. You need to do this PR from Branch 1.2 because master switched to support Symfony 2.5.5+. More details why - is here Symfony2

66Ton99 added a commit that referenced this pull request Jan 31, 2016
Fixed validation_groups when option is a Closure #93
@66Ton99
Copy link
Collaborator

66Ton99 commented Jan 31, 2016

Merged in #99

@66Ton99 66Ton99 closed this Jan 31, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants