Skip to content

Conditional validation #165

@vova07

Description

@vova07

Hi there, first of all thanks for a great library.

I have a question about conditional validation, because my biggest problem is that the library is completely dependent on array keys.

Here is what I need:

$validator->when(function (array $values) { return isset($values['key1']); })->require('key2')->string();

I can add a new rule When which will check if one or another key is set and validate it there, but how can I require or skip a key when there is or not another key set ?

Here is how I do it atm:

$validator->optional('key')->callback(function ($value, array $values) use ($validator) {
    if (!isset($values['hey2']) || empty($values['hey2'])) {
        $validator->required('key')->string();
    }

    return true;
});

Is there a better way ?

Thanks!

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions