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

Regex validator throws exception on invalid input #14943

Closed
emil-nasso opened this issue Jun 11, 2015 · 9 comments
Closed

Regex validator throws exception on invalid input #14943

emil-nasso opened this issue Jun 11, 2015 · 9 comments

Comments

@emil-nasso
Copy link

I have this example, which i use in a small json-api where i use the form component to handle requests and validation.

->add('account', 'text', [
                'constraints' => [
                    new NotBlank(),
                    new Type('string'),
                    new Regex("/^XX\d+$/i")
                ]
            ])

I have a form field for an accountname. The format of this name is XX1234.

The validation above works great if the data being validated is a string. If i pass in an array instead of the string i will get an exception that is originating from here:

https://github.com/symfony/symfony/blob/2.8/src/Symfony/Component/Validator/Constraints/RegexValidator.php#L43

In my opinion the validation should just fail here instead of throwing an exception. I pass an array in, and try to run an regexp on it. It seems obvious to me that something is wrong with the input data then and the validation should fail.

Other validation constraints seems to work this way. Look at this example:

->add('disksize', 'text', [
                'constraints' => [
                    new NotBlank(),
                    new Type('numeric'),
                    new Range(['min' => 10, 'max' => 200])
                ]
            ])

This works. I get an validation error and no exceptions. Other validations contraints, like Length seems to have the same problem as Regex

I think it would be great if all constraints just failed if they were passed an array if they cant handle an array. Or is there something i am overseeing?

Is there some way i can work around this to make it fail validation if i pass in an array?

@Tobion
Copy link
Member

Tobion commented Jun 11, 2015

Ref. #5334, #4102, #13804 etc

@xabbuh
Copy link
Member

xabbuh commented Jun 12, 2015

Does it make sense to fix this per validator or does it need to be solved in the validation extension of the Form component?

@machee
Copy link

machee commented Jun 24, 2015

Similarly, Collection should just fail instead of throwing an exception when not passed an array/ArrayAccess/Traversable.

@xabbuh
Copy link
Member

xabbuh commented Dec 16, 2016

By the way, what I don't understand here: Shouldn't such a request already fail before the validation when the submitted data are mapped to the form? I mean how should an array be mapped to a TextType?

@emil-nasso
Copy link
Author

I agree, that would make more sense.

@xabbuh xabbuh added the Form label Dec 27, 2016
@xabbuh
Copy link
Member

xabbuh commented Dec 27, 2016

#20935 when being applied more generally should fix this issue.

@stoccc
Copy link
Contributor

stoccc commented Jul 10, 2018

I think that a validator should fail validation if it not receives the expected format; it should not throw an exception..

The same thing happens in LengthValidator here https://github.com/symfony/validator/blob/master/Constraints/LengthValidator.php#L37 and in many other validators. What is the reason of this choice?

@yceruto
Copy link
Member

yceruto commented Jul 10, 2018

Also related to #13560

@xabbuh xabbuh removed the Form label Sep 13, 2018
@xabbuh
Copy link
Member

xabbuh commented Sep 24, 2018

Thank you all for the discussion. I am going to close here in favour of #12312.

@xabbuh xabbuh closed this as completed Sep 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants