validator: Validator requires one attribute and cannot see the whole model

What steps will reproduce the problem?

In yii2, it was easy to access any of the model’s attributes. In yii3, In Yii3 this option seems to me not feasible.

https://www.yiiframework.com/doc/guide/2.0/en/input-validation#standalone-validators

https://github.com/yiisoft/validator/blob/725fdfc3c18d8ef0fee3ed80fd73abe417620d77/src/Validator.php#L36

This issue also affects the Result class because it is not suitable for adding errors to an attribute.

What is the expected result?

I get the whole dataset in an extra parameter, and change return type to ResultSet

public function validate($value, DataSetInterface $dataSet): ResultSet
// or yii2 style
public function validate(?string $attribute, DataSetInterface $dataSet): ResultSet

https://github.com/yiisoft/validator/blob/725fdfc3c18d8ef0fee3ed80fd73abe417620d77/src/Rule.php#L10

https://github.com/yiisoft/validator/blob/725fdfc3c18d8ef0fee3ed80fd73abe417620d77/src/Result.php#L19

https://github.com/yiisoft/validator/blob/725fdfc3c18d8ef0fee3ed80fd73abe417620d77/src/ResultSet.php#L12

usage:

[
  /* numeric index -> attribute=null */ (new MyComplexRule())->moreParameters()...
]

What do you get instead?

[
  'notRelevantAttributeName' => (new MyComplexRule())->with($this /* or $dataset ??*/)->moreParameters()...
]

But in this case, I have no way of returning an arbitrary attribute error of the model.

Additional info

Also, to keep the benefits of the current solution, the Rule class should be somehow allow the Result class to be used internally in simpler cases.

Use case

I need to check for additional attributes depending on a type attribute.

Q A
Version 3.0.x-dev
PHP version -
Operating system -

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 35 (26 by maintainers)

Commits related to this issue

Most upvoted comments

By the end of the month or around it.

A validator for a single value is basically assertion. If we can have both that would be good but I suspect that it may complicate library too much.

I think this is mandatory rather than optional. For example, if I make my own validator that uses additional validator, I will usually validate single values.

@kamarton That’s OK but how do you plan getting data set into Rule?

  • add Rule::isSupportDataSet():bool
  • modify Validator

https://github.com/yiisoft/validator/blob/4d504bce7e81f3823831bf163a013f92c174187a/src/Validator.php#L31-L39

With the Rules class, I don’t know how. Special cases could be treated so that only classes from the Rules inheritance can use the dataset. This is also logical, because in this case, more than one rule applies.

class Address extends Rules
{
  public function isSupportDataSet() {    <---- maybe put it here ??
    return true;
  }
}

This solution implies that Validator::$attributeRules cannot have an attribute index. The attribute name must be moved one dimension deeper.

https://github.com/yiisoft/validator/blob/4d504bce7e81f3823831bf163a013f92c174187a/src/Validator.php#L8-L13