如何在验证消息Laravel 5.2中获取数组索引

时间:2016-04-12 11:14:02

标签: laravel-5.2 laravel-validation

我将这些数组放入Laravel Validator作为参数:

['item.*' => 'string'] // rules

['item.*.string' => 'Item number (index) is not string'] // messages

我希望验证邮件中包含index number。上面的代码仅用于演示,不起作用。怎么做?

1 个答案:

答案 0 :(得分:6)

试试这个或使用这个

    'name' : [ { 'value' : 'raju' } , { 'value' : 'rani'} ]       

并通过

验证
    'name.*' or 'name.*.value' => 'required|string|min:5'       

消息将是

    'name.*.required' => 'The :attribute is required'       
    'name.*.value.required' => 'The :attribute is required'      

我认为这会对你有所帮助..

试试这个,

public function messages()
{
    $messages = [];
    foreach ($this->request->get('name') as $key => $value){
        $messages['name.'. $key .'.required'] = 'The item '. $key .'  is not string';
    }
    return $messages;
}