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??

3
  • 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? Commented Mar 27, 2016 at 23:08
  • yes . i need a specific message from the error. Commented Mar 27, 2016 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. Commented Mar 27, 2016 at 23:23

3 Answers 3

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.'
]);
Sign up to request clarification or add additional context in comments.

1 Comment

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 should help you as well.
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
                    })
            }
        }
    });
});

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.