简体   繁体   English

Laravel 8:表单验证无法正常工作

[英]Laravel 8: Form Validation Does Not Work Properly

I have a form like this:我有这样的表格:

<form action="{{ route('profile.2fa.phone') }}">
    @csrf
    <div class="form-group">
        <label for="token" class="col-form-label">Token</label>
        <input type="text" class="form-control @error('token') is-invalid @enderror" name="token" placeholder="enter your token">
        @error('token')
            <span class="invalid-feedback">
                <strong>{{ $message }}</strong>
            </span>
        @enderror
    </div>
    <div class="form-group">
        <button class="btn btn-primary">Validate token</button>
    </div>
</form>

In this form, if the input field was empty this class="form-control @error('token') is-invalid @enderror" should make a red rectangle around the form input, and also, {{ $message }} should return the message The token field is required .在此表单中,如果输入字段为空,则class="form-control @error('token') is-invalid @enderror"应该在表单输入周围制作一个红色矩形,并且{{ $message }}应该返回消息The token field is required

Now the problem is, it does not work at all, I mean when you leave the input empty, and click on the button.现在的问题是,它根本不起作用,我的意思是当您将输入留空时,然后单击按钮。 there is no error reporting on page somehow.页面上没有错误报告。

So my question is, how to solve this problem and validate this form so a user can not leave the input empty?所以我的问题是,如何解决这个问题并验证这个表单,这样用户就不能把输入留空?

UPDATE: Controller ProfileController has two methods related to this blade:更新: Controller ProfileController有两种与此刀片相关的方法:

public function getPhoneVerify()
{
    return view('profile.phone-verify');
}

public function postPhoneVerify(Request $request)
{
    $request->validate([
        'token' => 'required'
    ]);


    return $request->token;
}

And on web.php these two routes handle the requests:web.php上,这两条路由处理请求:

Route::get('profile/twofactor/phone', [App\Http\Controllers\ProfileController::class, 'getPhoneVerify'])->name('profile.2fa.phone');
Route::post('profile/twofactor/phone', [App\Http\Controllers\ProfileController::class, 'postPhoneVerify']);

Bootstrap is buggy.引导程序是错误的。 Try changing this line:尝试更改此行:

<span class="invalid-feedback">

to:至:

<span class="text-danger">

and see if it works or not.看看它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM