yiisoft / yiisoft/validator

Callable rule attribute interface

Open
#466 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status:under discussion type:enhancement
Dominant language
PHP
Stars
167
Forks
47
Avg merge
1d 19h
Merged PRs (30d)
3

Description

Callable rule class may look like this:

final class MyRule {
    public function __invoke(mixed $value, object $rule, ValidationContext $context): Result {
        // ...
    }
}

For simple custom rules is good solution, but there is one problem. It is not possible usage as attribute.

We can add attribute Attribute:

#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
final class MyRule {
    // ...

It's not enough, because validator parse attributes that implement RuleInterface only.

Suggest add new interface CallableRuleAttributeInterface and parse implementing its attributes in addition to RuleInterface.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files or tests. Start by tracing the validator's attribute parsing and its checks for RuleInterface, then review the callable rule shape shown in the issue. Done means callable rule attributes are recognized alongside RuleInterface attributes, including the proposed repeatable class and property usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.