【发布时间】:2020-09-18 10:01:26
【问题描述】:
我正在尝试使用 Laravel Validator::make 验证用户注册表单。该代码有效,包括在相应字段中显示错误消息。
但我的问题是:-
1)无论我在电子邮件字段中输入什么,总是会产生错误。因此,即使我输入了正确的格式 example@example.com,它也不会将我重定向到下一页。我尝试删除“电子邮件”验证器并尝试过。它仍然显示“需要电子邮件字段”消息
2) 除此之外,如果一个或多个字段有任何错误,页面会被重定向回来,但输入字段不是 填满了过去的输入。
这里是控制器页面
public function showdata(Request $request){
$first_name = $request->first_name;
$last_name = $request->last_name;
$email = $request->eid;
$password = $request->password;
$confirm_password = $request->confirm_password;
$mobno = $request->mobno;
$dob = $request->dob;
$gender = $request->gender;
$address = $request->address;
$country = $request->country;
$time = $request->dt;
$messages=
[
'required' => "The :attribute field is required",
'email.email' => "The :attribute :input format should be example@example.com/.in/.edu/.org....",
'email.unique' => "The :attribute :input is taken. Please use another email address",
'confirm_password.same' => "Password and Confirm password fields must match exactly",
'mobno.digits' => "The :attribute field accepts only numbers",
'mobno.digits:10' => "The :attribute should be 10 digits long",
'dob.date_format' => "The date format :input should be YYYY-MM-DD",
'address.string' => "The :attribute :input must be in the form of a string"
];
$rules = [
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required|min:8',
'confirm_password' => 'required|min:8|same:password',
'mobno' => 'required|digits:10',
'dob' => 'required|date|date_format:Y-m-d',
'gender' => 'required',
'address' => 'required|string',
'country' => 'required'
];
$validate = Validator::make($request->all(),$rules,$messages);
if($validate->fails()){
return back()->withInput()->withErrors($validate->messages());
}
else{
return view('recheck_form')
->with('first_name',$first_name)
->with('last_name',$last_name)
->with('email',$email)
->with('password',$password)
->with('mobno',$mobno)
->with('dob',$dob)
->with('gender',$gender)
->with('address',$address)
->with('country',$country)
->with('time',$time);
}
}
}
刀片页面
<div class="container pt-2">
<div class="row shadow p-3 mb-5 bg-info rounded text-center text-white">
<div class="col ">
<h2>New User Registration</h2>
</div>
</div>
</div>
<div class="row m-5 p-5 bg-light text-info">
<div class="col">
<form class="form-group" method="post" action="recheck-form" autocomplete="off">
<div class="form-group {{ $errors->has('first_name') ? ' has-error' : '' }}">
@csrf
<label for="fname">First Name:</label>
<input type="text" class="form-control" name="first_name">
<small class="text-danger">{{$errors->first('first_name') }}</small>
</div>
<div class="form-group {{ $errors->has('last_name') ? ' has-error' : '' }}">
<label for="lname">Last Name:</label>
<input type="text" class="form-control" name="last_name">
<small class="text-danger">{{ $errors->first('last_name') }}</small>
</div>
<div class="form-group {{ $errors->has('email') ? ' has-error' : '' }}">
<label for="eid">Email/Username:</label>
<input type="text" class="form-control" name="eid">
<small class="text-danger">{{ $errors->first('email') }}</small>
</div>
<div class="form-group {{ $errors->has('password') ? ' has-error' : '' }}">
<label for="pwd">Password:</label>
<input type="password" class="form-control" name="password">
<small class="text-danger">{{$errors->first('password') }}</small>
</div>
<div class="form-group {{ $errors->has('confirm_password') ? ' has-error' : '' }}">
<label for="confpwd">Confirm Password:</label>
<input type="password" class="form-control" name="confirm_password">
<small class="text-danger">{{ $errors->first('confirm_password') }}</small>
</div>
<div class="form-group {{ $errors->has('mobno') ? ' has-error' : '' }}">
<label for="mobno">Mobile Number:</label>
<input type="text" class="form-control" name="mobno">
<small class="text-danger">{{ $errors->first('mobno') }}</small>
</div>
<div class="form-group {{ $errors->has('dob') ? ' has-error' : '' }}">
<label for="dob">Date of Birth(in YYYY-MM-DD):</label>
<input type="text" class="form-control" name="dob">
<small class="text-danger">{{ $errors->first('dob') }}</small>
</div>
<div class="form-group {{ $errors->has('gender') ? ' has-error' : '' }}">
<label for="gender">Gender:</label>
<br>
<input class="form-check-input" type="radio" name="gender" id="Female" value="Female">
<label class="form-check-label" for="Female">
Female
</label>
<input class="form-check-input" type="radio" name="gender" id="Male" value="Male">
<label class="form-check-label" for="Male">
Male
</label>
<input class="form-check-input " type="radio" name="gender" id="Other" value="Other">
<label class="form-check-label" for="Other">
Other
</label>
<small class="text-danger">{{ $errors->first('gender') }}</small>
</div>
<div class="form-group {{ $errors->has('address') ? ' has-error' : '' }}">
<label for="address">Address:</label>
<textarea class="form-control" rows="5" name="address"></textarea>
<small class="text-danger">{{ $errors->first('address') }}</small>
</div>
<div class="form-group {{ $errors->has('country') ? ' has-error' : '' }}">
<label for="country">Country:</label>
<select name="country" class="form-control" id="countrylist">
<option value disabled selected>Select Country</option>
@foreach($countryname as $key => $country)
<option id="countryname" value="{{$country->countryname}}">{{$country->countryname}}</option>
@endforeach
</select>
<small class="text-danger">{{ $errors->first('country') }}</small>
</div>
<div class="form-group">
<label for="dt">Date and Time of Submission:</label>
<input type="text" class="form-control" name="dt" readonly value=@php date_default_timezone_set("Asia/Kolkata"); echo date("Y-m-d,H:i:s ") @endphp>
</div>
<div class="form-group text-center">
<input type="submit" class="btn btn-primary mb-2">
</div>
我不知道如何将图像上传到 Stack Overflow 问题。如果你能告诉我,我可以通过图片告诉你确切的问题。
【问题讨论】:
-
我希望
name="eid"是name="email",因为这就是'email' => 'required|email|unique:users'将要寻找的规则。您可以尝试更新它以及$email = $request->email;并再试一次吗? -
或者,您可以将规则更改为
'eid' => 'required|email|unique:users',并将模板代码更改为$errors->has('eid')和$errors->first('eid'),但这对我来说似乎很奇怪。 -
eid="email/userid" 上次我尝试从“eid”更改为“email”有效。但是,我仍然不明白“开斋节”有什么问题。此外,如果有任何错误消息,它会显示它们,但 inpur 字段没有以前的数据
-
您可以通过单击编辑器上的图像选项来添加屏幕截图。
-
@Asish 在下面添加了上面的答案以及在错误重定向上显示先前值的详细信息