8000 Regex validator throws exception on invalid input · Issue #14943 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Regex validator throws exception on invalid input #14943
Closed
@emil-nasso

Description

@emil-nasso

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0