Marin Bînzari
Marin Bînzari

Reputation: 5348

Symfony2 validator Length with min parameter allows empty value

Today I found something strange about Symfony >=2.3 validators. I you use

$metadata->addPropertyConstraint('body', new Length(array('min' => 50)));

it allows empty inputs. I don't think it should do this or am I wrong?

Upvotes: 3

Views: 1437

Answers (1)

rpg600
rpg600

Reputation: 2850

This is the expected behaviour.

Just think about the different use cases: a phone number can be optional, but if the user has entered a phone number, it should be at least 10 characters.

So you need to combine your Length constraint with a NotNull/NotBlank constraint.

See https://github.com/symfony/symfony/issues/10221#issuecomment-34769066 for more explanation.

Btw : there is actually a brainstorming about empty string handling https://github.com/symfony/symfony/issues/11956.

Upvotes: 2

Related Questions