-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[8.x] Add key/index/iteration placeholders to validation attributes #35754
Conversation
…teration_to_attribute_names
Why iterate over the same thing three times? |
@taylorotwell I can merge all of them in one function. |
Is |
Maybe something like replaceArrayPlaceholders. |
I dunno, I think I'm going to skip this one. I would just make a wildcard language line that says something like "All entires require a name" instead of having something like "The name (2)(1) field is required." |
I know I can use something like this.
The above message is just a sample created by me for tests and can be replaced with more readable attributes/messages instead of I provide more examples, hopefully, I don't need to use the old way I using now to approach this functionality anymore. Another example: // Sample request ['items' => [4, 8, 'test', 3]]
public function test(Request $request)
{
Validator::make($request->all(),
['items.*' => 'required|numeric|min:1'],
[],
['items.*' => 'Item in row :iteration']
)->validate();
// The Item in row 3 must be a number.
} Validator::make(
['name' => [['', 'Foo'], ['', 'Bar']]],
['name.*.*' => 'required'],
['name.*.*.required' => 'The field in row :iteration0 and column :iteration1 is required.']
)->validate();
// Error message 1: The field in row 1 and column 1 is required.
// Error message 2: The field in row 2 and column 1 is required. |
This pull request provides 3 placeholders for validation attributes/messages when validating arrays
and replaces them for better validation messages.
:key
: which is array key.:index
: which is the same key but replaces only numbers (not strings).:iteration
: which is the same index plus one (which more readable and useful for the final user).An example code before this change:
Same code after this change:
There are placeholders for nested arrays too and can be handle by adding an index number at end of the placeholder.