samedi 11 février 2017

Laravel 5.1 - Return First Error From FormRequest Validation

I have the following form request:

class FileRequest extends Request
{

   public function authorize()
   {
      return true;
   }

   public function rules()
   {

      $rules = [];

      if($this->file_type == 'image')
         $rules['file'] = 'required|image|mimes:jpg,jpeg,gif|max:244|image_size:>=360,>=180';
    else
         $rules['file'] = 'required|mimes:doc,pdf,docx,txt,rtf|max:1000';

      return $rules;

   }

   public function messages()
   {
      $messages = [
        'file.image_size' => 'The image size is incorrect.'
      ];

      return $messages;
   }

   public function response(array $errors)
   {
        $content = "<textarea data-type=\"application/json\">{\"ok\": false, \"message\": \"" . $errors[0] . "\" }</textarea>";
         return response($content);

    }



}

How do i return the first error from the errors array.

When I manually create a validator I'm able to do the following: $validator->errors()->first() but this doesn't work when using a FormRequest class. Doing errors[0] simply gives me an offset exception error.

I'm using an iframe submit which is why I need to return the response as above.

Any help appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire