0

我是 laravel 的新手,我正在创建一个登录系统,用户可以使用用户名或电子邮件登录。当我提交表单时,它返回错误The username field is required.

登录请求.php

public function rules()
    {
        return [
            Fortify::username() => 'required|string',
            'password' => 'required|string',
        ];
    }

FortifyServiceprovider.php

Fortify::authenticateUsing(function (LoginRequest $request) {
            $user = User::where('email', $request->username)
                ->orWhere('username', $request->username)->first();

    if ($user && Hash::check($request->password, $user->password)) {
      return $user;
    });

强化.php

'features' => [
        Features::registration(),
        Features::resetPasswords(),
        Features::emailVerification(),
        Features::updateProfileInformation(),
        Features::updatePasswords(),
        Features::twoFactorAuthentication([
            'confirmPassword' => true,
        ]),
    ],

登录.blade.php

<form class="mt-4" action="login" method="POST">
@csrf
<div class="mb-3">
   <label class="form-label" for="username">Username</label>
   <input type="text" value="{{ old('username') }}" class="form-control @error('username') is-invalid @enderror" id="username" placeholder="Enter Username">
   @error('username')
   <span class="invalid-feedback" role="alert">
   <strong>{{ $message }}</strong>
   </span>
   @enderror
</div>
<div class="mb-3">
   <label class="form-label" for="userpassword">Password</label>
   <input type="password" class="form-control @error('password') is-invalid @enderror" id="userpassword"
      placeholder="Enter password">
   @error('password')
   <span class="invalid-feedback" role="alert">
   <strong>{{ $message }}</strong>
   </span>
   @enderror
</div>
</form>

请任何帮助将不胜感激。谢谢

4

1 回答 1

0

将名称属性添加到输入字段用户名和密码

登录.blade.php

    <form class="mt-4" action="login" method="POST">
@csrf
<div class="mb-3">
   <label class="form-label" for="username">Username</label>
   <input type="text" name="username" value="{{ old('username') }}" class="form-control @error('username') is-invalid @enderror" id="username" placeholder="Enter Username">
   @error('username')
   <span class="invalid-feedback" role="alert">
   <strong>{{ $message }}</strong>
   </span>
   @enderror
</div>
<div class="mb-3">
   <label class="form-label" for="userpassword">Password</label>
   <input type="password" name="password" class="form-control @error('password') is-invalid @enderror" id="userpassword"
      placeholder="Enter password">
   @error('password')
   <span class="invalid-feedback" role="alert">
   <strong>{{ $message }}</strong>
   </span>
   @enderror
</div>
</form>
于 2021-08-07T22:52:10.220 回答