How to get array index in validation message Laravel 5.2

人走茶凉 提交于 2021-02-07 08:37:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!