简体   繁体   中英

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

I am trying to validate user registration form using Laravel Validator::make . The code works, including showing the error messages in the respective fields. But my problems are :-

1) No matter what I type in the email field, it always produces an error . So, it does not redirects me to the next page, even if I type the correct format example@example.com . I tried removing the "email" validator and tried. It still shows me "email field is required" message

2) In addition to that,if there are any errors in one or more fields,the page gets redirected back but the input fields are not filled up with the past input .

Here is the Controller page

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);
}

}
}

And the Blade Page

<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>



I dont know how to upload images to Stack Overflow questions. If you can tell me, I can show you the exact problem via images.

First of all, you are doing wrong in your code. In the validator rules and messages array, you did write wrong.

Please check the below code It will resolve your issue.

    $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'
];

The page gets redirected back but the input fields are not filled up with the past input. For this issue you can try the below approach will definitely work

$validate =  Validator::make($request->all(),$rules,$messages);
 

if($validate->fails()){
    return redirect()->back()->withErrors($validate->messages())->withInput();
}

And in your input value attribute please write like this 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') }}" />

The names of your field and rules dont match. please update

"name="eid" to be name="email" as that's what the rule 'email' => 'required|email|unique:users' will look for.

Then also update $email = $request->email;

In order to reset the previously entered values after the error re-direct, please update your view like this:

In view:

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

Note,

If you want to keep it as eid , thats fine too, you just need to make sure everything uses eid , ie:

<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',

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM