laravel-5laravel-validation

How to get array index in validation message Laravel 5.2


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?


Solution

  • 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;
    }