I want to be able to show errors using jquery after validation but I have no idea how to access the object sent to my controller; so I dont even know what to 'return' in the controller. Can someone please walk me through this?
This is a part of my view
<meta name="_token" content="{{ csrf_token() }}" />
<div class='row'>
{!! Form::open(['url'=>'register','id'=>'sign-up','class'=>'col-md-6 col-md-push-4 form-horizontal'])!!}
<div class='form-group'>
{!! Form::label('first_name', 'First Name:',['class'=>'col-xs-12 col-md-3']) !!}
<div class= 'col-xs-12 col-md-6'>
{!! Form::text('first_name', null, ['class' => 'form-control'])!!}
</div>
</div>
<div class='form-group'>
{!! Form::label('last_name', 'Last Name:',['class'=>'col-xs-12 col-md-3']) !!}
<div class= 'col-xs-12 col-md-6'>
{!! Form::text('last_name', null, ['class' => 'form-control'])!!}
</div>
</div>
<div class='form-group'>
{!! Form::label('email', 'Email Address:',['class'=>'col-xs-12 col-md-3']) !!}
<div class= 'col-xs-12 col-md-6 '>
{!! Form::text('email', null, ['class' => 'form-control'])!!}
<div class='form-group'>
{!! Form::label('password', 'Password:',['class'=>'col-xs-12 col-md-3']) !!}
<div class= 'col-xs-12 col-md-6'>
{!! Form::password('password', null, ['class' => 'form-control'])!!}
</div>
</div>
<div class='form-group'>
{!! Form::label('password_confirmation', 'Confirm Password:',['class'=>'col-xs-12 col-md-3']) !!}
<div class= 'col-xs-12 col-md-6'>
{!! Form::password('password_confirmation', null, ['class' => 'form-control'])!!}
</div>
</div>
</div> <div class='btn btn-small'>
{!! Form::submit('Join Us!',['class'=>'btn btn-success btn-sm form-control'])!!}
</div>
{!! Form::close() !!}
</div>
.js file:
$(function(){
$('#sign-up').on('submit',function(e){
$.ajaxSetup({
header:$('meta[name="_token"]').attr('content')
})
e.preventDefault(e);
$.ajax({
type:"POST",
url:'/register',
data:$(this).serialize(),
dataType: 'json',
success: function(data){
console.log(data);
},
error: function(data){
}
})
});
});
controller:
<?php
namespace App\Http\Controllers;
use App\Http\Requests\CreateRegisterRequest;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\HttpResponse;
use Input;
class UserController extends Controller
{
public function create(CreateRegisterRequest $request)
{
}
public function show()
{
return view('user.profile');
}
}
Form Request:
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class CreateRegisterRequest extends Request
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'first_name' =>'required',
'last_name'=>'required',
'url'=>'url',
'description',
'email'=>'unique:users,email|email',
'password'=>'min:6|confirmed',
'password_confirmation'=>'min:6'
];
}
}