我尝试使用ajax进行注册,使用laravel验证数据。 我意识到发送,但不是如何返回验证错误并在js代码中接收错误并在div中显示错误。
我正在使用5.2版本的laravel。谢谢你的帮助:)
JS:
function send(event){
event.preventDefault();
$.ajax({
type: 'post',
url: "{{route('NewUser')}}",
dataType: 'json',
data: {
name: $('#name').val(),
email: $('#email').val(),
password: $('#password').val(),
cpassword: $('#cpassword').val(),
_token: $('#new-user').attr('data-token')
},
success: function(errors){
$('#Register').modal('hide');
},
error: function(errors) {
}
});
}
laravel controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
use App\Http\Controllers\Controller;
use App\Http\Requests;
class UserController extends Controller
{
public function index()
{
return view('home');
}
public function NewUser(Request $request)
{
$this->validate($request,[
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:8|same:cpassword',
'cpassword'=> 'required|min:8'
]);
CreateNewUSer($request);
}
Protected function CreateNewUser(Request $request){
}
答案 0 :(得分:1)
您的验证错误将以json的形式返回,因此您可以执行类似
的操作 $.ajax({
type: 'post',
url: url,
data: data,
dataType: 'json',
success: function(data){
// success logic
}),
error: function(data){
var errors = data.responseJSON;
console.log(errors);
// Render the errors with js ...
}
});
所以如果出错,您将从验证中获得所有错误。
答案 1 :(得分:1)
为了达到目标,我建议您使用FormRequest
首先创建表单请求类
php artisan make:request UserRequest
然后在rules
UserRequest
方法中添加一些规则
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:8|same:cpassword',
'cpassword'=> 'required|min:8',
];
}
为简单起见,所有用户都可以提出此请求(如果您希望以这种方式检查用户权限,则应查看文档)
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
进一步在您的控制器中
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Http\Requests\UserRequest;
class UserController extends Controller
{
public function index()
{
return view('home');
}
public function NewUser(UserRequest $request)
{
// laravel will validate request itself automatically and
// will go on if validate pass or will return back with
// status code 422, input and errors otherwise
CreateNewUSer($request);
}
Protected function CreateNewUser(Request $request){
}
最后,您可以使用jQuery的ajax .error()
function send(event){
event.preventDefault();
$.ajax({
type: 'post',
url: "{{route('NewUser')}}",
dataType: 'json',
data: {
name: $('#name').val(),
email: $('#email').val(),
password: $('#password').val(),
cpassword: $('#cpassword').val(),
_token: $('#new-user').attr('data-token')
},
success: function(errors){
$('#Register').modal('hide');
},
error: function(errors) {
//handle errors here
}
});
}
修改强> 在使用ajax函数的js之前将其放入视图中
<script>
var userAjax = "{{ route('NewUser') }}";
</script>
和你的ajax js
$.ajax({
type: 'post',
url: userAjax,
dataType: 'json',
// ...
答案 2 :(得分:0)
$objValidator = Validator::make(Input::all(), array(
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:8|same:cpassword',
'cpassword'=> 'required|min:8'
),array(
'required'=>'Required',
'email'=>'Invalid email',
'unique' => 'exists'
));
if($objValidator->fails())
{
return $objValidator->errors();
}
在你的js代码中获取数组而不是 使用$ .each函数可以显示错误。 例如,您的输入框下方有一个div html:
<div id="email_error" class="error" style="display:none">Email Already Exits.</div>
$.each(data, function (key,val) {
$("#"+key+'_error').show();
});