//http://code4fun.io/post/laravel-ajax-register-and-login
#view
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
<html lang="en"> <head> <title>Custom Validation Rule Laravel 5</title> <meta name="_token" content="{{ csrf_token() }}" /> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" > <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script> $.ajaxSetup({ headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }, }) </script> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="#">Custom Validation Rule Laravel 5</a> </div> </div> </nav> <div class="container"> <form id='sign-up' action="{{ URL::to('customValiPost') }}" class="form-horizontal" method="post" novalidate> {{csrf_token()}} @if (count($errors) > 0) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <!--<input type="text" name="title" class="form-control" style="width:30%" placeholder="Add Odd String" />--> <div class="form-group" id="email-div"> <label class="control-label" for="title">title</label> <input id="title" type="text" placeholder="example@gmail.com" title="Please enter you email" required value="" name="title" class="form-control"> <span class="help-block"> <strong id="form-errors-title"></strong> </span> <span class="help-block small">Your email</span> </div> </span> @if ($errors->has('title')) <span class="help-block"> <strong>{{ $errors->first('title') }}</strong> </span> @endif <br/> <button class="btn btn-primary">Save</button> </form> <script> $(function(){ $('#sign-up').on('submit',function(e){ $.ajaxSetup({ headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }, }) e.preventDefault(e); $("#title-div").removeClass("has-error"); $.ajax({ type:"POST", url:'/laravel1/public/ajax', data:$(this).serialize(), //dataType: 'json', success: function(data){ console.log(data); }, error: function(data){ console.log(data); var obj = JSON.parse(data.responseText); if (obj.title) { $("#title-div").addClass("has-error"); $('#form-errors-title').html(obj.title); } } }) }); }); </script> </div> </body> </html> |
#route
Route::get(‘/’,’CustomValDemoController@customVali’);
Route::post(‘/ajax’,’CustomValDemoController@ajax’);
#conroller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php /** * Created by IntelliJ IDEA. * User: Akash * Date: 1/25/2017 * Time: 9:37 PM */ namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Input; use Illuminate\Validation\Validator; use App\Http\Requests\TestRequest; class CustomValDemoController extends Controller { public function customVali() { return view('customVali'); } public function customValiPost(Request $request) { /* $this->validate($request, [ 'title' => 'required|min:2|max:5|validateFoo', ]);*/ print_r('done'); } public function ajax(Request $request){ $this->validate($request, [ 'title' => 'required', ]); if($request->ajax()){ return response()->json(['responseText' => 'Success!'], 200); } return 'hola'; } } |