php - Allow empty strings as well as the specified validation assertions

653

So I'm using Symfony validation to make sure a posted field is within the given range. I also want to allow empty string values in this field, for example if the user does not supply any data.

'somePostedField' => [
    new Assert\Optional(
        new Assert\Range([
            'min'        => 0.01,
            'max'        => 100,
            'minMessage' => 'tooShort',
            'maxMessage' => 'tooLong',
            'invalidMessage' => 'invalid',
        ])
    )
],

You'd think the Optional assertion would do the trick, but that just ignores the rules for the field if it is not present in the data when validating.

How do I also allow empty strings in this field?

People are also looking for solutions to the problem: php - CakePHP 3.0 Confirm Delete Modal in Bake Template

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.