Repository navigation
Allow multiple fields to use the same ruleset in validation #62321
Unanswered
bluesheep100
asked this question in
Ideas
Replies: 1 comment
|
I doubt, there is interest to add this Alternatively, you can do this: class MyRequest extends FormRequest
{
const BASE_RULES = 'required|integer|min:0';
public function rules(): array
{
return [
/* other rules */
'foo' => self::BASE_RULES,
'bar' => self::BASE_RULES,
'baz' => self::BASE_RULES,
'buz' => self::BASE_RULES,
];
}
}or class MyRequest extends FormRequest
{
const BASE_RULES = 'required|integer|min:0';
public function rules(): array
{
return array_merge(
[/* other rules */],
array_fill_keys(['foo', 'bar', 'baz', 'buz'], self::BASE_RULES),
);
}
}Self promoThere is a package, that allows the definition of compound rules in one go: |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I have a system where several models have multiple fields that need the same basic validation, for example:
My suggestion is, essentially to instead allow the following as a shorthand:
So these calls are identical, except the latter does not require you to repeat the rules for every field.
Of course, while cumbersome, you can already do this:
I also think that the shorthand should allow you to "elaborate" on any given field, for example:
Error messages should also be reported as normal, e.g. a failure on
barbeing missing still falls under['errors' => ['bar' => 'The "bar" field is required.']].There should also be no issue with backwards-compatibility to adding this, and it will allow for better reuse of rules, without creating wrappers around the validation system, on top of integrating well with the existing Request classes.
Would love to hear some feedback on this idea.
All reactions