Laravel 最大验证无法正常工作

Laravel max validation not working properly

大家好,我正在尝试在 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

发生了什么问题?

让我在这里澄清一件事,当您为数字创建验证时,您使用的最大值和最小值将用于最小值和最大值。

因此您可以输入 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