Skip to content

Instantly share code, notes, and snippets.

@mindesik
Last active May 28, 2016 00:02
Show Gist options
  • Save mindesik/b37851fd72f993edc38a287555a96f01 to your computer and use it in GitHub Desktop.
Save mindesik/b37851fd72f993edc38a287555a96f01 to your computer and use it in GitHub Desktop.
Laravel ajax and php request with errors validation
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}, // required
method: //
data: //
url: //
}).error(function (response) {
if (response.responseJSON.errors) {
console.log(response.responseJSON.errors);
}
}).always(function () {
console.log('response returned');
}).success(function () {
console.log('request success');
});
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
use Illuminate\Contracts\Validation\Validator;
class EntryRequest extends Request {
/**
* Custom errors validator.
*
* @access protected
* @param Validator $validator
* @return array
*/
protected function formatErrors(Validator $validator)
{
return ['errors' => $validator->errors()->all()];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment