问题
These arrays I put into Laravel Validator
as arguments:
['item.*' => 'string'] // rules
['item.*.string' => 'Item number (index) is not string'] // messages
I want to have index number
in validation message. Code above is just for demonstration and does not work. How to do this?
回答1:
Try this or use this one
'name' : [ { 'value' : 'raju' } , { 'value' : 'rani'} ]
and validate it by
'name.*' or 'name.*.value' => 'required|string|min:5'
The message will be
'name.*.required' => 'The :attribute is required'
'name.*.value.required' => 'The :attribute is required'
I think it will help to you..
Try this one,
public function messages()
{
$messages = [];
foreach ($this->request->get('name') as $key => $value){
$messages['name.'. $key .'.required'] = 'The item '. $key .' is not string';
}
return $messages;
}
来源:https://stackoverflow.com/questions/36571628/how-to-get-array-index-in-validation-message-laravel-5-2