@@ -463,9 +463,10 @@ $data = [
463463
464464##### Basic usage
465465
466- Common flow is the same as you would use usual classes:
467- 1 . Declare property
468- 2 . Add rules to it
466+ Common flow is the same as you would use usual classes:
467+
468+ 1 . Declare property.
469+ 2 . Add rules to it.
469470
470471``` php
471472use Yiisoft\Validator\Rule\Count;
@@ -536,75 +537,48 @@ final class Post
536537}
537538```
538539
539- ##### Limitations
540-
541- ###### ` Callback ` rule and ` callable ` type
540+ ##### Callbacks
542541
543- ` Callback ` rule is not supported, also you can't use ` callable ` type with attributes. Use custom rule instead.
542+ ` Callback::$callback ` property is not supported, also you can't use ` callable ` type with attributes. However,
543+ ` Callback::$method ` can be set instead:
544544
545545``` php
546- use Attribute;
547- use Yiisoft\Validator\Exception\UnexpectedRuleException;
546+ <?php
547+
548+ declare(strict_types=1);
549+
550+ namespace Yiisoft\Validator\Tests\Stub;
551+
548552use Yiisoft\Validator\Result;
549- use Yiisoft\Validator\Rule\Number;
550- use Yiisoft\Validator\RuleHandlerInterface;
551- use Yiisoft\Validator\RuleInterface;
553+ use Yiisoft\Validator\Rule\Callback;
552554use Yiisoft\Validator\ValidationContext;
553555
554- #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
555- final class ValidateXRule implements RuleInterface
556+ final class Author
556557{
557- public function __construct()
558- {
559- }
560- }
558+ #[Callback(method: 'validateName')]
559+ private string $name;
561560
562- final class ValidateXRuleHandler implements RuleHandlerInterface
563- {
564- public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result
561+ public static function validateName(mixed $value, object $rule, ValidationContext $context): Result
565562 {
566- if (!$rule instanceof ValidateXRule) {
567- throw new UnexpectedRuleException(ValidateXRule::class, $rule);
568- }
569-
570563 $result = new Result();
571- $result->addError('Custom error.');
564+ if ($value !== 'foo') {
565+ $result->addError('Value must be "foo"!');
566+ }
572567
573568 return $result;
574569 }
575570}
576-
577- final class Coordinates
578- {
579- #[Number(min: -10, max: 10)]
580- #[ValidateXRule()]
581- private int $x;
582- #[Number(min: -10, max: 10)]
583- private int $y;
584- }
585571```
586572
587- ###### ` GroupRule `
588-
589- ` GroupRule ` is not supported, but it's unnecessary since multiple attributes can be used for one property.
573+ Note that the method must exist and have public and static modifiers.
590574
591- ``` php
592- use Yiisoft\Validator\Rule\HasLength;
593- use Yiisoft\Validator\Rule\Regex;
594-
595- final class UserData
596- {
597- #[HasLength(min: 2, max: 20)]
598- #[Regex('~[a-z_\-]~i')]
599- private string $name;
600- }
601- ```
575+ ##### Limitations
602576
603577###### Nested attributes
604578
605579PHP 8.0 supports attributes, but nested declaration is allowed only in PHP 8.1 and above.
606580
607- So such attributes as ` Each ` , ` Nested ` and ` Composite ` are not allowed in PHP 8.0.
581+ So attributes such as ` Each ` , ` Nested ` and ` Composite ` are not allowed in PHP 8.0.
608582
609583The following example is not allowed in PHP 8.0:
610584
@@ -659,8 +633,8 @@ final class Color
659633
660634###### Function / method calls
661635
662- You can't use a function / method call result with attributes. Like with ` Callback ` rule and callable, this problem can
663- be overcome with custom rule.
636+ You can't use a function / method call result with attributes. This problem can be overcome either with custom rule or
637+ ` Callback::$method ` property. An example of custom rule:
664638
665639``` php
666640use Attribute;
0 commit comments