Laravel 8-旧输入和验证错误消息不起作用

如果对某一输入的验证与指定字段上的上一次输入一起失败,我会尝试在该字段下方显示错误div。

以下是我到目前为止的进展:
在我的controller文件中store函数(UserController > store)

public function store(Request $request)
{
    $validationRules = array(
        'first_name' => 'required|min:2|max:150',
        'last_name' => 'required|min:2|max:150',
        'gender' => 'required',
        'birthdate' => 'required',
        'user_contact' => 'required|min:10|max:20',
        'email' => 'required|unique:users|max:150'
    );

    $validator = Validator::make($request->all(), $validationRules);

    if($validator->fails()){
        //dd($validator)
        return redirect(route('user.create'))->withInput()->withErrors($validator);
    }

.....

当我尝试使用dd()打印$validator时,它没有任何问题,并且它包含验证错误消息、所有输入和验证规则。

以下是create.blade.php (view)

中的代码片段
<form class="form-horizontal" method = "POST" action = "{{ route('user.store') }}">
        @csrf
            <div class="form-group row">
                <div class="col-md-12">
                    <strong><font color="red">*</font> Indicates required fields.</strong>
                </div>
                <div class="col-md-4">
                    <label for="u_fname" class="col-sm-6 col-form-label">First Name <font color="red">*</font></label>
                    <div class="input-group mb-2 {{$errors->has('u_fname') ? 'has-error' : ''}}" >
                        <div class="input-group-prepend">
                            <span class="input-group-text"><i class="fa fa-user" aria-hidden="true"></i></span>
                        </div>
                        <input type="text" 
                                class="form-control" 
                                id="first_name" 
                                name = "first_name"
                                value="{{ old('first_name') }}"
                                minlength="2"
                                maxlength="150"
                                required/>
                        @error('first_name')
                            <div class="alert-danger">{{$errors->first('first_name') }} </div>
                        @enderror
                    </div>
                </div>
                <div class="col-md-4">
                    <label for="u_lname" class="col-sm-6 col-form-label">Last Name <font color="red">*</font></label>
                    <div class="input-group mb-2 {{$errors->has('u_lname') ? 'has-error' : ''}}" >
                        <div class="input-group-prepend">
                            <span class="input-group-text"><i class="fa fa-user" aria-hidden="true"></i></span>
                        </div>
                        <input type="text" 
                                class="form-control" 
                                id="last_name" 
                                name = "last_name"
                                value="{{ old('last_name') }}"
                                minlength="2"
                                maxlength="150"
                                required/>
                        @error('last_name')
                            <div class="alert-danger">{{$errors->first('last_name') }} </div>
                        @enderror
                    </div>
                </div>
                <div class="col-md-4">
                    <label for="u_gender" class="col-sm-6 col-form-label">Gender <font color="red">*</font></label>
                    <div class="input-group mb-2 {{$errors->has('u_gender') ? 'has-error' : ''}}" >
                        <div class="input-group-prepend">
                            <span class="input-group-text"><i class="fas fa-venus-mars"></i></span>
                        </div>
                        <select class = "form-control" id = "gender" name = "gender" required>
                            <option value = "M">Male</option>
                            <option value = "F">Female</option>
                        </select>
                        @error('gender')
                            <div class="alert-danger">{{$errors->first('gender') }} </div>
                        @enderror
                    </div>
                </div>
                <div class="col-md-4">
                    <label for="u_birthdate" class="col-sm-6 col-form-label">Birth Date <font color="red">*</font></label>
                    <div class="input-group mb-2 {{$errors->has('u_birthdate') ? 'has-error' : ''}}" >
                        <div class="input-group-prepend">
                            <span class="input-group-text"><i class="far fa-calendar-alt"></i></span>
                        </div>
                        <input type="text" 
                                class="form-control" 
                                id="birthdate" 
                                name = "birthdate"
                                value="{{ old('birthdate') }}"
                                required/>
                        <div class="alert-danger">{{$errors->first('birthdate') }} </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <label for="u_contact" class="col-sm-6 col-form-label">Contact Number <font color="red">*</font></label>
                    <div class="input-group mb-2 {{$errors->has('u_contact') ? 'has-error' : ''}}" >
                        <div class="input-group-prepend">
                            <span class="input-group-text"><i class="fas fa-phone"></i></span>
                        </div>
                        <input type="text" 
                                class="form-control" 
                                id="user_contact" 
                                name = "user_contact"
                                value="{{ old('user_contact') }}"
                                onkeyup="this.value=this.value.replace(/[^d]/,'')"
                                minlength="10"
                                maxlength="20"
                                required/>
                        @error('user_contact')
                            <div class="alert-danger">{{$errors->first('user_contact') }} </div>
                        @enderror
                    </div>
                </div>
                <div class="col-md-4">
                    <label for="u_email" class="col-sm-6 col-form-label">E-Mail Address<font color="red">*</font></label>
                    <div class="input-group mb-2 {{$errors->has('u_email') ? 'has-error' : ''}}" >
                        <div class="input-group-prepend">
                            <span class="input-group-text"><i class="fas fa-at"></i></span>
                        </div>
                        <input type="email" 
                                class="form-control" 
                                id="email" 
                                name = "email"
                                value="{{ old('email') }}"
                                minlength="10"
                                maxlength="150"
                                required/>
                        @error('email')
                            <div class="alert-danger">{{$errors->first('email') }} </div>
                        @enderror
                    </div>
                </div>
                <div class="col-md-4">
                    <label for="u_utype" class="col-sm-6 col-form-label">User Type <font color="red">*</font></label>
                    <div class="input-group mb-2 {{$errors->has('u_utype') ? 'has-error' : ''}}" >
                        <div class="input-group-prepend">
                            <span class="input-group-text"><i class="fas fa-user-cog"></i></span>
                        </div>
                        <select class = "form-control" id = "u_type_input" name = "u_type_input" required>
                            <option value = "E">Employee</option>
                            <option value = "A">Administrator</option>
                        </select>
                        @error('u_type_input')
                            <div class="alert-danger">{{$errors->first('u_type_input') }} </div>
                        @enderror
                    </div>
                </div>
            </div>
        </div>
        <div class="card-footer">
            <button type="submit" class="btn btn-default float-right" name = "submit_create" id = "submit_create">Submit</button>
        </div>
        </form>

请注意,我删除了autocomplete=off,因为我阅读了一些帖子,认为这会导致问题,但在我的情况下仍然不起作用。

我已经阅读了所有与我的问题相关的Stackoverflow帖子,但大多数场景问题都发生在Laravel 5上,我使用的是Laravel框架的最新版本,也就是Laravel 8。

问题的原因似乎是什么?以及我应该如何保留 旧/以前的输入,并在失败后显示错误消息 验证?我怀疑问题出在刀片式服务器上 文件或与引导等资源相关的文件。


解决方案

您可以尝试此重定向

        if ($validator->fails())
        {
            return redirect()->back()->withErrors($validator->errors());
        }

在刀片文件中显示时,尝试这个

 @error('user_contact')
        <div style="color: red;">{{ $message }}</div>
 @enderror

相关文章