Laravel 最大验证无法正常工作
Posted
技术标签:
【中文标题】Laravel 最大验证无法正常工作【英文标题】:Laravel max validation not working properly 【发布时间】:2020-07-17 04:11:10 【问题描述】:大家好,我正在尝试在 laravel 7 中构建用户管理系统,但是在我的注册表单中,我的手机号码输入不断出现验证错误。
这是我的注册表
<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>
这是我的控制器
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']),
]);
问题是我现在验证了我的手机号码字段最多只能包含 12 个字符
'mobile'=>['required','numeric', 'min:10','max:12'],
但是当我每次运行我的代码时,我都会收到一条验证错误消息,
"The mobile may not be greater than 12."
我的输入是,0774566678
出了什么问题?
【问题讨论】:
【参考方案1】:让我在这里澄清一件事,当您为数字创建验证时,您正在使用的 max 和 min 用于最小值和最大值。
所以你可以输入一个从 10 到 12 的值。
'mobile'=>['required','numeric', 'min:10','max:12'],
因此,您需要删除数字验证规则并按以下方式使用。
'mobile' => 'required|string|min:10|max:12|regex:/[0-9]9/'
或在确切字符的情况下
'mobile' => 'required|string|size:10|regex:/[0-9]9/'
这是参考。链接 https://laravel.com/docs/7.x/validation#rule-size
【讨论】:
以上是关于Laravel 最大验证无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章