我有一个bootstrap模式但是这里服务器端验证没有显示所以我试图在JQuery validate plugin中验证,我试图检查电子邮件,如果它在JQuery验证中是唯一的,但它仍然给我错误消息和不会提交
控制器:
public function register(Request $request){
$validation =$this->validate($request, [
'name' => 'required',
'email' => 'required|email|unique:users,'
'password' =>'Required|AlphaNum|Between:6,8|Confirmed',
'password_confirmation' =>'Required|AlphaNum|Between:6,8'
]);
$users = new User([
'name' => $request['name'],
'email' => $request['email'],
'password' => bcrypt($request['password']),
]);
$users->save();
return redirect()->route('product.index')
->with('success', 'welcome');
}
//
public function validateEmail(Request $request)
{
$validator = Validator::make($request->all(), [
'email' => 'required|email|unique:users',
]);
if($validator->fails()){
return Response::json(array(
'fail' => true,
'errors' => $validator->getMessageBag()->toArray()
));
}else {
return Response::json(array(
'success' => true,
'email' => $request->input('email')
));
}
}
JavaScript的:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$(".register").submit(function (e) {
e.preventDefault();
var form=$('.registerform');
$.ajax({
type: "POST",
url: $form.attr('action'),
data:form.serialize(),
dataType: 'json',
success: function(result) {
if (response.status == "OK") {
console.log ('success');//Success!
$('#register').modal('hide');
$('.register').attr("disabled", false);
} else {
console.log ('failure');//Fail!
$(".errors").html(result.errors);
$('.register').attr("disabled", false);
}
}
});
});
$.validator.addMethod("remote",
function(value, element) {
var result = false;
$.ajax({
type:"get",
url: "/email", // script to validate in server side
data:form.serialize(),
});
// return true if username is exist in database
return result;
},
"This email is already taken! Try another."
);
$.validator.addMethod("PASSWORD",function(value,element){
return this.optional(element) || /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,8}$/i.test(value);
},"Passwords are 6,8 characters with uppercase letters, lowercase letters and at least one number.");
$(".register").click(function () {
form = $(".registerform");
$('.registerform').validate({
wrapper: "li",
rules:{
name:{
required:true,
},
password:{
required:true,
PASSWORD : true
},
// password_confirmation: {required: true},
email:{
required:true,
email:true,
remote:true
}
},
messages:{
name:{
required:"Please provide your Login",
},
password:{
required:"Please provide your password",
},
email:{
required:"Please provide your email adress",
email:"it should be an email",
//remote: "This email is already taken! Try another."
},
///password_confirmation:{
required:"Please provide your password",
// equalTo:"it should be equal to password"
// }
}
});
});