onlinecode

Laravel Ajax Request with Validation Example

Laravel Ajax Request with Validation Example

In this post we will give you information about Laravel Ajax Request with Validation Example. Hear we will give you detail about Laravel Ajax Request with Validation ExampleAnd how to use it also give you demo for it if it is necessary.

In this example, I am going to show you how to make validation using jquery ajax in laravel 5 application.

It would be always greate if you use laravel validation for your web form in laravel project. we can use laravel validation very simply if we don’t need to use jquery ajax. because laravel provide easy way to use validation without ajax. But if you want to use laravel validation with jquery then you can’t do it easily. However you can make it by following example.

In this example i will show you how to use laravel default validation with jquery ajax. Here we also print laravel validation message when false. So if you want to ajax form validation in laravel app then you are right place.

See also 

How to solve 'There are no commands defined in the "cashier" namespace' in Laravel 5

In this post we will give you information about How to solve 'There are no commands defined in the "cashier" namespace' in Laravel 5. Hear we will give you detail about How to solve 'There are no commands defined in the "cashier" namespace' in Laravel 5And how to use it also give you demo for it if it is necessary.



When i did start to learn "Laravel Cashier" and i was following step by step of laravel official document, but i run bellow command :

php artisan cashier:table users

At that time i found following error on my terminal:

[SymfonyComponentConsoleExceptionCommandNotFoundException] 

There are no commands defined in the "cashier" namespace.

I try to solve this error, i also clear all cache and add service provider, it means i try totally and i can't found solution for this error, but you can also add migration like this way if you want it is a same like we did bellow command. so first run command for create migration:

php artisan make:migration add_cashier_table_fields

ok, now put bellow content on following file :

Migration:

use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;

class AddBillTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::table('users', function ($table) {

$table->string('stripe_id')->nullable();

$table->string('card_brand')->nullable();

$table->string('card_last_four')->nullable();

$table->timestamp('trial_ends_at')->nullable();

});

Schema::create('subscriptions', function ($table) {

$table->increments('id');

$table->integer('user_id');

$table->string('name');

$table->string('stripe_id');

$table->string('stripe_plan');

$table->integer('quantity');

$table->timestamp('trial_ends_at')->nullable();

$table->timestamp('ends_at')->nullable();

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{


}

}

Hope this code and post will helped you for implement How to solve 'There are no commands defined in the "cashier" namespace' in Laravel 5. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

In bellow i will give you very simple example and full example so it will be great to understand. So let’s just follow few step and you get example like as bellow preview:

Preview:

Step 1: Add Route

In first step we will create new two routes for demo. so open your routes/web.php file and add following route.

routes/web.php

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

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

Step 2: Create Controller

In this point, now we should create new controller as HomeController. So run bellow command and create new controller.

php artisan make:controller HomeController

After bellow command you will find new file in this path app/Http/Controllers/HomeController.php.

In this controller we will write three method for ajax view and post as listed bellow methods:

1)myform()

2)myformPost()

So, let’s copy bellow code and put on HomeController.php file.

app/Http/Controllers/HomeController.php

<?php


namespace AppHttpControllers;


use IlluminateHttpRequest;

use Validator;


class HomeController extends Controller

{


/**

* Display a listing of the myform.

*

* @return IlluminateHttpResponse

*/

public function myform()

{

return view('myform');

}


/**

* Display a listing of the myformPost.

*

* @return IlluminateHttpResponse

*/

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()]);

}

}


Also see:Laravel 5 create Custom Validation Rule example.

Step 3: Create View File

In Last step, let’s create myform.blade.php(resources/views/myform.blade.php) for layout and we will write design code and jquery ajax code,so put following code:

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 >

<h2>Laravel Ajax Validation</h2>


<div style="display:none">

<ul></ul>

</div>


<form>

{{ csrf_field() }}

<div >

<label>First Name:</label>

<input type="text" name="first_name" placeholder="First Name">

</div>


<div >

<label>Last Name:</label>

<input type="text" name="last_name" placeholder="Last Name">

</div>


<div >

<strong>Email:</strong>

<input type="text" name="email" placeholder="Email">

</div>


<div >

<strong>Address:</strong>

<textarea name="address" placeholder="Address"></textarea>

</div>


<div >

<button >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 we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

Also see:Ajax Image Upload Example with Validation in PHP Laravel Framework

http://localhost:8000/my-form

I hope it can help you…

Hope this code and post will helped you for implement Laravel Ajax Request with Validation Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version