Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[12.x] IntroduceScopeAwareRule contract to provide relative contextual array item data to validation rules#58077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
lucasacoutinho wants to merge1 commit intolaravel:12.x
base:12.x
Choose a base branch
Loading
fromlucasacoutinho:feat/scope-aware-rule

Conversation

@lucasacoutinho
Copy link

When writing custom validation rules for array items, accessing sibling data requires manual path manipulation. Consider an API for bulk order creation:

{    "orders": [        {            "customer_id": 123,            "items": [                {                    "product_id": 1,                    "quantity": 10,                    "unit_price": 150.00,                    "discount": 2000.00                }            ]        }    ]}

To validate that discount doesn't exceed the line total (quantity x unit_price), you currently need:

class DiscountMustNotExceedLineTotalimplements ValidationRule, DataAwareRule{protectedarray$data = [];publicfunctionsetData(array$data):static    {$this->data =$data;return$this;    }publicfunctionvalidate(string$attribute,mixed$value,Closure$fail):void    {// $attribute = "orders.0.items.1.discount"// Need to extract indices and navigate the nested structurepreg_match('/^orders\.(\d+)\.items\.(\d+)\./',$attribute,$matches);$orderIndex =$matches[1] ??null;$itemIndex =$matches[2] ??null;if ($orderIndex ===null ||$itemIndex ===null) {return;        }$item =$this->data['orders'][$orderIndex]['items'][$itemIndex] ?? [];$lineTotal = ($item['quantity'] ??0) * ($item['unit_price'] ??0);if ($value >$lineTotal) {$fail("The discount cannot exceed the line total of{$lineTotal}.");        }    }}

We could make the experience much nicer with a new and more limited ScopeAwareRule interface that automatically receives the current array item's data:

class DiscountMustNotExceedLineTotalimplements ValidationRule, ScopeAwareRule{protectedarray$scope = [];publicfunctionsetScope(array$scope):static    {$this->scope =$scope;return$this;    }publicfunctionvalidate(string$attribute,mixed$value,Closure$fail):void    {$lineTotal = ($this->scope['quantity'] ??0) * ($this->scope['unit_price'] ??0);if ($value >$lineTotal) {$fail("The discount cannot exceed the line total of{$lineTotal}.");        }    }}

@lucasacoutinholucasacoutinho marked this pull request as draftDecember 10, 2025 23:58
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

@lucasacoutinho

[8]ページ先頭

©2009-2025 Movatter.jp