Skip to content

Commit 6e2049a

Browse files
committed
Replace {id} placeholder in rules with the model's key
1 parent 7f73189 commit 6e2049a

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/functions_laravel.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,31 @@ function rules($rules, array $messages = [], array $customAttributes = []): Clos
2525
$rules = [$rules];
2626
}
2727

28-
return function (callable $fail, $value, $model, Context $context, Field $field) use ($rules, $messages, $customAttributes) {
28+
return function (callable $fail, $value, Model $model, Context $context, Field $field) use (
29+
$rules,
30+
$messages,
31+
$customAttributes
32+
) {
2933
$key = $field->getName();
30-
$validationRules = [$key => []];
31-
$validationMessages = [$key => []];
3234

33-
foreach ($rules as $k => $v) {
34-
if (! is_numeric($k)) {
35-
$validationRules[$key.'.'.$k] = $v;
36-
} else {
37-
$validationRules[$key][] = $v;
38-
}
39-
}
35+
$validatorRules = [$key => []];
36+
37+
foreach ($rules as $k => $rule) {
38+
$rule = str_replace('{id}', $model->getKey(), $rule);
4039

41-
foreach ($messages as $k => $v) {
4240
if (! is_numeric($k)) {
43-
$validationMessages[$key.'.'.$k] = $v;
41+
$validatorRules[$key.'.'.$k] = $rule;
4442
} else {
45-
$validationMessages[$key][] = $v;
43+
$validatorRules[$key][] = $rule;
4644
}
4745
}
4846

49-
$validation = Validator::make($value !== null ? [$key => $value] : [], $validationRules, $validationMessages, $customAttributes);
47+
$validation = Validator::make(
48+
$value !== null ? [$key => $value] : [],
49+
$validatorRules,
50+
$messages,
51+
$customAttributes
52+
);
5053

5154
if ($validation->fails()) {
5255
foreach ($validation->errors()->all() as $message) {

0 commit comments

Comments
 (0)