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
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
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
- Fix #28: Implement context aware validation — committed to yiisoft/validator by samdark 5 years ago
- Fix #28: Implement context aware validation — committed to yiisoft/validator by samdark 5 years ago
By the end of the month or around it.
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.
Rule::isSupportDataSet():boolValidatorhttps://github.com/yiisoft/validator/blob/4d504bce7e81f3823831bf163a013f92c174187a/src/Validator.php#L31-L39
With the
Rulesclass, I don’t know how. Special cases could be treated so that only classes from theRulesinheritance can use the dataset. This is also logical, because in this case, more than one rule applies.This solution implies that
Validator::$attributeRulescannot 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