Hello all I'm trying to build user management system in laravel 7, But in my registration form I'm keep getting a validation error for my mobile number input.
This is my registration form
<form method="POST" action="{{ route('register') }}">
@csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
@error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="user_type" class="col-md-4 col-form-label text-md-right">{{ __('You Are a') }}</label>
<div class="col-md-6">
<select id="user_type" class="form-control @error('user_type') is-invalid @enderror" name="user_type">
<option value="">-Select your type-</option>
<option value="Shop Owner">Shop Owner</option>
<option value="Customer">Customer</option>
</select>
@error('user_type')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email">
@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('Mobile Number') }}</label>
<div class="col-md-6">
<input id="mobile" type="text" class="form-control @error('mobile') is-invalid @enderror" name="mobile" value="{{ old('mobile') }}" required autocomplete="email">
@error('mobile')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password">
@error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Register') }}
</button>
</div>
</div>
</form>
And this is my controller
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'mobile'=>['required','numeric', 'min:10','max:12'],
'user_type'=>['required','string'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'mobile'=>$data['mobile'],
'user_type'=>$data['user_type'],
'password' => Hash::make($data['password']),
]);
}
The issue is now I have validated my mobile number field for maximum of 12 characters only
'mobile'=>['required','numeric', 'min:10','max:12'],
But when I run my code every time I'm getting an validation error message saying,
"The mobile may not be greater than 12."
My input was, 0774566678
What was the issue?
Let me clear one thing here is that when you create validation for numeric digits then max and min that one you are using is used for minimum and maximum value.
So you can enter a value from 10 to 12.
'mobile'=>['required','numeric', 'min:10','max:12'],
So you will need to remove numeric valiadation rule and use as follow.
'mobile' => 'required|string|min:10|max:12|regex:/[0-9]{9}/'
or in case of exact characters
'mobile' => 'required|string|size:10|regex:/[0-9]{9}/'
Here is the ref. links https://laravel.com/docs/7.x/validation#rule-size