yiisoft / validator

Yii validator library
https://www.yiiframework.com/
BSD 3-Clause "New" or "Revised" License
110 stars 36 forks source link

Way to add error with custom value path #651

Open vjik opened 5 months ago

vjik commented 5 months ago

In rule handlers Result::addError() is added error message to current key and value path define path into key. Need ability to add error with globally path.

Example:

$result = (new Validator())->validate(
    data: ['a' => 1, 'b' => 2],
    rules: [
        'a' => new Callback(function(){
            $result = new Result();
            $result->addError('message', valuePath: ['b']);
            return $result;
        })
    ],
);

// Errors:
// ['a.b' => ['message']]

Need additional ability add error to key b. May be implement it via adding method addError() to validation context object.

samdark commented 5 months ago

What's the use-case?

vjik commented 5 months ago

What's the use-case?

Forms: add error to another field or add error without fields (common errors).