5

In Laravel 5.1 Project

I'm getting this Ajax Response As Error

{
      "success":false,
      "errors":{
      "drugs_power":["The drugs power field is required."]
      }
}

i need a error validation like this image enter image description here i can make it by laravel validator by this code(return From Controller),

$this->validate($request, [
            'drugs_name' => 'required|unique:drugs',
            'drugs_group_name' => 'required',
            'drugs_power' => 'required'
        ]);

Show In blade.php by this code

<div class="form-group{{ $errors->has('drugs_power') ? ' has-error' : '' }}">
   <label for="userName">Power</label>
       <div>
          <input type="text" class="form-control" name="drugs_power"
                                               value="{{old('drugs_power')}}">
        @if ($errors->has('drugs_power'))
          <span class="help-block">
         <strong>{{ $errors->first('drugs_power') }}</strong>
              </span>
           @endif
     </div>


</div>

How can i make ajax+laravel validation just like that??

K M Rifat ul alom
  • 525
  • 12
  • 25
  • What is it that you're trying to do exactly? What you have above seems normal, no? Are you trying to return a specific message from the error? – camelCase Mar 27 '16 at 23:08
  • yes . i need a specific message from the error. – K M Rifat ul alom Mar 27 '16 at 23:13
  • 2
    Whatever you are using to perform ajax submit (jQuery?), it also has to be programmed in such a way to process the error data and then append the error message to your HTML. There is no magic in Laravel that will do it for you. – Oliver Maksimovic Mar 27 '16 at 23:23

3 Answers3

1

Passing in a 3rd parameter to validate allows you to specify a custom message, as seen here. You can use "dot" notation to specify the error message to a specific field.

$this->validate($request, [
    'drugs_name' => 'required|unique:drugs',
    'drugs_group_name' => 'required',
    'drugs_power' => 'required'
], [
    'drugs_power.required' => 'Your custom message here.'
]);
camelCase
  • 5,460
  • 3
  • 34
  • 37
  • Laravel will return a `JSON` response containing all of the validation errors upon failing validation with AJAX requests. It returns this response with status `422` so you can access the errors there. [This answer](http://stackoverflow.com/questions/11092007/check-what-error-value-is-in-ajax-callback) should help you as well. – camelCase Mar 27 '16 at 23:23
0

Assuming u are using the Laravel request object correctly you will recieve a response code 422 in your browser.

If you like to catch this globally you can extend the default ajax behavior, by putting following code in your JS.

$(function () {
    $.ajaxSetup({
        statusCode: {
            422: function (data) {
                    $.each(data.responseJSON, function (k, v) {
                      // do something with the error
                    })
            }
        }
    });
});
Frederiek
  • 1,653
  • 1
  • 16
  • 27
0

You are looking for https://github.com/whipsterCZ/laravel-ajax

It does exactly what you want and much more!

sending and Validating forms via ajax is simple like this - no configuration needed

HTML

<form action="" class="ajax">...</form>

Controller

public function update(ClientRequest $request, Client $client)
{
    $client->update($request->all());
    $request->session()->flash('success', 'Client has been updated.');

    return \Ajax::redirect(route('clients.index'));
}

It validate form (through custom FormRequest) and shows errors (in errorBag or directly above inputs)

WhipsterCZ
  • 638
  • 1
  • 8
  • 13