【问题标题】:In Laravel ,how to validate all the input fields, display error messages if any, and return back to the form with the inputs if there are errors在 Laravel 中,如何验证所有输入字段,显示错误消息(如果有),并在有错误时返回输入的表单
【发布时间】: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>
  &nbsp;&nbsp;&nbsp;&nbsp;
  <input class="form-check-input" type="radio" name="gender" id="Female" value="Female">
  <label class="form-check-label" for="Female">
    Female
  </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

  <input class="form-check-input" type="radio" name="gender" id="Male" value="Male">
  <label class="form-check-label" for="Male">
    Male
  </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
  <input class="form-check-input " type="radio" name="gender" id="Other" value="Other">
  <label class="form-check-label" for="Other">
    Other
  </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  <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' =&gt; 'required|email|unique:users' 将要寻找的规则。您可以尝试更新它以及$email = $request-&gt;email; 并再试一次吗?
  • 或者,您可以将规则更改为'eid' =&gt; 'required|email|unique:users',并将模板代码更改为$errors-&gt;has('eid')$errors-&gt;first('eid'),但这对我来说似乎很奇怪。
  • eid="email/userid" 上次我尝试从“eid”更改为“email”有效。但是,我仍然不明白“开斋节”有什么问题。此外,如果有任何错误消息,它会显示它们,但 inpur 字段没有以前的数据
  • 您可以通过单击编辑器上的图像选项来添加屏幕截图。
  • @Asish 在下面添加了上面的答案以及在错误重定向上显示先前值的详细信息

标签: php laravel


【解决方案1】:

首先,您在代码中做错了。在验证器规则和消息数组中,您确实写错了。

请检查下面的代码它将解决您的问题。

    $messages=
         [
      'eid.required'                =>   "The :attribute field is required",
      'eid.email'             =>    "The :attribute :input format should be example@example.com/.in/.edu/.org....",
       'eid.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',
               'eid' => '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 redirect()->back()->withErrors($validate->messages())->withInput();
}

并且在您的输入值属性中请这样写 value="{{ old('name') }}"

<input type="text" name="name" value="{{ old('name') }}" />
<input type="email" name="email" value="{{ old('email') }}" />
<input type="text" name="first_name" value="{{ old('first_name') }}" />

【讨论】:

  • 是的。有效。谢谢你。但是,既然你在这里,你能告诉我,如果我点击后退按钮,如何确保表单输入保留它们的旧数据?
  • 您需要在单击后退按钮时再次在同一控制器和方法上重定向
  • @Asish 你能告诉我为什么你不接受这个答案。
【解决方案2】:

您的字段名称和规则不匹配。请更新

"name="eid" 变为 name="email",因为这就是 'email' =&gt; 'required|email|unique:users' 将寻找的规则。

然后也更新$email = $request-&gt;email;

为了在错误重定向后重置之前输入的值,请像这样更新您的视图:

在视图中:

<input type="text" class="form-control" name="email" value="{{ old('email') }}">

注意,

如果你想保持为eid,也可以,你只需要确保所有内容都使用eid,即:

<div class="form-group {{ $errors->has('eid') ? ' has-error' : '' }}">
 <label for="eid">Email/Username:</label>
  <input type="text" class="form-control" name="eid" value="{{ old('eid') }}">
  <small class="text-danger">{{ $errors->first('eid') }}</small>
</div>




$email = $request->eid;


'eid.email' => "The :attribute :input format should be example@example.com/.in/.edu/.org....",
'eid.unique' => "The :attribute :input is taken. Please use another email address",



'eid' => 'required|email|unique:users',

【讨论】:

  • 第一部分已经完成。但是,即使在更新代码之后,我仍然得到空输入字段(在重定向错误消息之后)
  • @Asish 使用 old('email')
  • @AlzafanChristian 啊,很好,我把它当作视图渲染,而不是重定向:)
  • @Wesley Smith 你能解释一下那是什么(查看渲染等)我基本上是 Laravel 的新手。
  • @Asish 不考虑,我的答案的先前版本建议将数据发送到视图,如图所示 here 但我忘记了代码中的错误案例使用了类似 back()-&gt;withInput() 的重定向,Alzafan指出了我的错误,并且使用old('email') 是在重定向后访问这些值的正确方法:)
猜你喜欢
  • 2021-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多