我为我的用户创建了一个注册页面,但电子邮件验证不起作用,我无法显示电子邮件已被使用的警报。我应该对代码进行哪些更改
控制器代码
public function registerUser(Request $request)
{
// Validate form input
$validator = Validator::make($request->all(), [
'first_name' => 'required',
'middle_name' => 'required',
'last_name' => 'required',
'birthdate' => 'required',
'email' => 'required|email|unique:user_models,email',
'password' => 'required|min:5|max:20'
], [
'first_name.required' => 'First name field is required',
'middle_name.required' => 'Middle name field is required',
'last_name.required' => 'Last name field is required',
'birthdate.required' => 'Birthdate field is required',
'email.required' => 'Email field is required',
'email.email' => 'Email field should be a valid email',
'email.unique' => 'The email has already been taken',
'password.required' => 'Password field is required',
'password.min' => 'Password should be at least 5 characters',
'password.max' => 'Password should be at most 20 characters',
]);
if ($validator->fails()) {
// Return validation error if input is invalid
return response()->json([
'status' => 400,
'errors' => $validator->messages(),
'message' => 'Please fix the following errors:',
])->withErrors($validator)->with('error', 'Please fix the following errors:');
}
else {
// Create new user model and save to database
$user = new UserModel();
$user->first_name = Str::ucfirst($request->input('first_name'));
$user->middle_name = Str::ucfirst($request->input('middle_name'));
$user->last_name = Str::ucfirst($request->input('last_name'));
$user->ip_address = $request->ip('ip_address');
$user->birthdate = $request->input('birthdate');
$user->email = Str::lower($request->input('email'));
$user->account_status = '1';
$user->password = Hash::driver('whirlpool')->make($request->password);
$user->save();
$seq_id = $user->seq_id;
$user->application_code ="AUP-M231-" . str_pad($seq_id, 6, '0', STR_PAD_LEFT);
$user->save();
// Create new ApplicantPPCModel and save to database
$user2 = new ApplicantPPCModel();
$user2->email = Str::lower($request->input('email'));
$user2->pc = $request->input('password');
$user2->seq_id = $user->seq_id;
$user2->save();
// Send activation email to user
$data = [
'title' => 'Welcome to Lorem Ipsum University',
'body' => 'Thank you for registering with us',
'activation_code' => $user->application_code,
'first_name' => $user->first_name
];
try {
Mail::to($user->email)->send(new MailNotify($data));
} catch (Swift_TransportException $e) {
// Return error response if email fails to send
return response()->json(['message' => 'Sorry! Please try again later'], 500);
}
// Return success response
return response()->json([
'status' => 200,
'message' => 'You have registered successfuly',
]);
}
}
** AJAX 代码**
$.ajax({
type: "POST",
url: "/register-user",
data: data,
dataType: "json",
success: function (response) {
if(response.status == 400) {
Swal.fire({
type: 'error',
title: 'Oops...',
text: 'That email is in use already. Please try another one',
})
}else {
$('.alert-success').removeClass('hideme');
setTimeout(function() {
$('.alert-success').fadeOut();
}, 3000);
$('#reg_first_name').val('');
$('#reg_middle_name').val('');
$('#reg_last_name').val('');
$('#reg_email').val('');
$('#birth_date').val('');
$('#reg_email_confirm').val('');
$('#reg_password').val('');
$('#reg_password_confirm').val('');
$('#reg_id_number').val('');
$( "#checkbox-signup" ).prop( "checked", false );
$( "#btn-register" ).prop( 'disabled', true );
Swal.fire("You're Registered!", "Please check your email before proceeding to the Login Page", "success")
}
}
});
网络消息“消息”:“SQLSTATE[23505]:唯一违规:7错误:重复键值违反了唯一约束“the_email”\n详细信息:注册码(电子邮件)=(loremipsum@gmail.com)已经存在。
我可以显示甜蜜的警报时,用户注册成功,但我不能显示任何错误消息,将提醒用户与注册。
2条答案
按热度按时间2izufjch1#
您的'email'的'unique'验证规则似乎不起作用,所以您得到SQLSTATE[23505],我认为是一个500响应。检查验证规则中的表名和列名,如果您正在更新,请添加一个忽略条件。
然后我想你可以像这样重构验证响应:
响应::类为Symfony\组件\HttpFoundation\响应
iyr7buue2#
因为当电子邮件已经存在时,您编写的验证不会处理它,因此验证不会失败,这意味着它会直接尝试创建新用户,但会失败,因为真实的上电子邮件已经存在于数据库中
解决方案:
1.验证表名是否正确
1.验证列名是否正确
1.用模型的路径替换表名
'email' =〉'唯一:应用程序\模型\用户模型,email'