HDTuto.com

Ajax validation in laravel 5.7 Example



In this tutorial, i will let you know how to use laravel validation using ajax request. we will use laravel validation for jquery ajax request in laravel 5.7. you will learn how to print error of laravel validation with jquery ajax.

Jquery also provide to write logic of validation like required, email etc with ajax easily, but if you need use laravel validation like unique, digits, date validation etc, then you must use laravel validation.

i will show you step step tutorial of ajax validation with laravel 5.7. you can also see how to print error after validation false.

just follow bellow code for ajax validation:

Create Route: routes/web.php

Route::get('my-form','HomeController@myform');

Route::post('my-form','HomeController@myformPost');

Create Controller: app/Http/Controllers/HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Validator;

class HomeController extends Controller

{

/**

* Display a listing of the myform.

*

* @return \Illuminate\Http\Response

*/

public function myform()

{

return view('myform');

}

/**

* Display a listing of the myformPost.

*

* @return \Illuminate\Http\Response

*/

public function myformPost(Request $request)

{

$validator = Validator::make($request->all(), [

'first_name' => 'required',

'last_name' => 'required',

'email' => 'required|email',

'address' => 'required',

]);

if ($validator->passes()) {

return response()->json(['success'=>'Added new records.']);

}

return response()->json(['error'=>$validator->errors()->all()]);

}

}

Create View File: resources/views/myform.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel Ajax Validation Example</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>

</head>

<body>

<div class="container">

<h2>Laravel Ajax Validation</h2>

<div class="alert alert-danger print-error-msg" style="display:none">

<ul></ul>

</div>

<form>

{{ csrf_field() }}

<div class="form-group">

<label>First Name:</label>

<input type="text" name="first_name" class="form-control" placeholder="First Name">

</div>

<div class="form-group">

<label>Last Name:</label>

<input type="text" name="last_name" class="form-control" placeholder="Last Name">

</div>

<div class="form-group">

<strong>Email:</strong>

<input type="text" name="email" class="form-control" placeholder="Email">

</div>

<div class="form-group">

<strong>Address:</strong>

<textarea class="form-control" name="address" placeholder="Address"></textarea>

</div>

<div class="form-group">

<button class="btn btn-success btn-submit">Submit</button>

</div>

</form>

</div>

<script type="text/javascript">

$(document).ready(function() {

$(".btn-submit").click(function(e){

e.preventDefault();

var _token = $("input[name='_token']").val();

var first_name = $("input[name='first_name']").val();

var last_name = $("input[name='last_name']").val();

var email = $("input[name='email']").val();

var address = $("textarea[name='address']").val();

$.ajax({

url: "/my-form",

type:'POST',

data: {_token:_token, first_name:first_name, last_name:last_name, email:email, address:address},

success: function(data) {

if($.isEmptyObject(data.error)){

alert(data.success);

}else{

printErrorMsg(data.error);

}

}

});

});

function printErrorMsg (msg) {

$(".print-error-msg").find("ul").html('');

$(".print-error-msg").css('display','block');

$.each( msg, function( key, value ) {

$(".print-error-msg").find("ul").append('<li>'+value+'</li>');

});

}

});

</script>

</body>

</html>

Now you can run and check it.




February 12, 2019 | Category : PHPLaravelJQueryAjax
Related Posts



Blog Search