Skip to content

Changing optional to required will not trigger errors when provided an empty string #142

@rick-nu

Description

@rick-nu

What

When making an optional field required if another field is set, the validation is not correctly executed in the following case:

    public function testCheckOptionalGetsRequiredValidation()
    {
        $this->validator->optional('foo')->lengthBetween(1, 2)->required(function ($values) {
            return array_key_exists('bar', $values);
        });

        $result = $this->validator->validate(['bar' => true, 'foo' => '']);

        $this->assertTrue($result->isNotValid());
    }

What is expected

Since the bar field exists in the array, the lengthBetween validation should be executed. It looks like the "allowEmpty" rule is not removed

Note

This only happens when you enter an empty string, if you change the value of foo to lol-lol, you will get a proper validation exception.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions