-
I often find cases where i am writing duplicate rules for form validations. Something along these sort of lines (not real code, just a basic example):
I think it would be great if this could work similar to a match where you can have multiple attributes in a single arm, like so:
I would love to give this a go myself but honestly wouldn't know where to start 😅 I appreciate it might not even possible / would be a breaking change to move away from passing in an array, I just think it could be a cool idea and couldn't find anyone else talking about it online. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
I'd create class constants for these. Or we can add Rule alias feature where you globally define "requiredEmail" => 'required|email'. |
Beta Was this translation helpful? Give feedback.
-
@jdarkins this is not valid php code. You forgot a set of []
But this is a syntax for match not for defining arrays. Maybe this is what you wanted (csv keys):
|
Beta Was this translation helpful? Give feedback.
-
Why not write the rules like this? $rules = [];
foreach (['name', 'add1', 'add2', 'add3', 'town', 'postcode'] as $attribute) {
$rules[$attribute] = ['required', 'string'];
}
$request->validate($rules + [
'email' => ['required', 'email'],
]); |
Beta Was this translation helpful? Give feedback.
I'd create class constants for these. Or we can add Rule alias feature where you globally define "requiredEmail" => 'required|email'.