我想用laravel表单验证来制作注册表。
在那种情况下,我面临一个很大的问题。
表单验证有效,但错误无法正确显示。
我在电子邮件和电话中看到以下错误:Validation.unique
必填字段:validation.required
这是我的控制器代码:
public function homeregister(Request $request)
{
$this->validation($request);
$request['password'] = bcrypt($request->password);
$email_verification = time();
$sms_verify_code = mt_rand(100000, 999999);
$name = $request->fname.' '.$request->lname;
$dob = $request->year.'-'.$request->month.'-'.$request->day;
User::create(['email'=>$request->email,'password'=>$request['password'],'fname'=>$request->fname,'lname'=>$request->lname,'gender'=>$request->gender,'dob'=>$dob,'religion'=>$request->religion,'country'=>$request->country,'email_verify_code'=>$email_verification,'phone'=>$request->phone,'sms_verify_code'=>$sms_verify_code,'name'=>$name,'identifier'=>$request->fname,'profile_created_by'=>$request->profile_for]);
$value = $request->email;
$phone = $request->phone;
Session::put('email', $value);
Session::put('phone', $phone);
$myuser = DB::table('users')->select()->where('email',Session::get('email'))->first();
DB::insert('insert into tbl_profile_photo (userid,photo) values(?,?)',[$myuser->id,null]);
DB::insert('insert into tbl_cover_photos (userid,photo) values(?,?)',[$myuser->id,null]);
\Mail::to($value)->send(new wellcome);
Auth::loginUsingId($myuser->id);
Session::put('userid',$myuser->id);
return redirect('/update-profile');
}
验证功能:
public function validation($request)
{
$validatedData = $request->validate([
'fname' => 'required|max:255',
'lname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'phone' => 'required|unique:users|max:255',
'gender' => 'required|max:255',
'year' => 'required|max:255',
'month' => 'required|max:255',
'day' => 'required|max:255',
'religion' => 'required|max:255',
'country' => 'required|max:255',
'password' => 'required|min:6',
]);
}
我的观看代码:
@if (count($errors) > 0)
@foreach ($errors->all() as $error)
<div class="alert alert-danger alert-dismissible mb-2" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<strong>Oh snap!</strong> {{$error}}
</div>
@endforeach
@endif
不明白吗?怎么了?
答案 0 :(得分:0)
您是否在数据库表中使用与电子邮件相同的名称?如果是,则唯一验证有效,否则将无效。