大家好,我正在尝试在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 AppUser
*/
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
问题出在哪里?
让我在这里澄清一件事,即当您为数字创建验证时,您使用的最大值和最小值将用于最小值和最大值。
因此,您可以输入一个从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-尺寸