[英]How do you customize the default login error message in laravel 8
I was browsing through the web and looking for solution on how can I modify this error message on Jetstream login:我正在浏览网页并寻找有关如何在 Jetstream 登录时修改此错误消息的解决方案:
Inside app/Actions/Fortify
there is a file CreateNewUser.php
where I can put some validation custom message on each field like this:在app/Actions/Fortify
有一个文件CreateNewUser.php
,我可以在其中在每个字段上放置一些验证自定义消息,如下所示:
public function create(array $input)
{
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'phone' => ['required', 'string', 'max:17'],
'password' => $this->passwordRules(),
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['required', 'accepted'] : '',
],[
'name.required'=>'Name is required',
'email.required'=>'Email is required',
'phone.required'=>'Phone is required',
])->validate();
However, there is none for Login.php
.但是, Login.php
没有。 I search the web and saw something about vendors/laravel/fortify/src/Actions/AttemptToAuthenticate.php
but its consist with lots of code where I don't know where to put the customization:我在网上搜索并看到了有关vendors/laravel/fortify/src/Actions/AttemptToAuthenticate.php
但它包含大量代码,我不知道在哪里放置自定义:
public function handle($request, $next)
{
if (Fortify::$authenticateUsingCallback) {
return $this->handleUsingCustomCallback($request, $next);
}
if ($this->guard->attempt(
$request->only(Fortify::username(), 'password'),
$request->filled('remember'))
) {
return $next($request);
}
$this->throwFailedAuthenticationException($request);
}
Is there an easy way to customize the "The email field is required" to a different custom message in Laravel 8?有没有一种简单的方法可以在 Laravel 8 中为不同的自定义消息自定义“电子邮件字段是必需的”?
Been stuck for a couple of hours.卡了几个小时。
You can customize FortifyServiceProvider
inside App > Providers
by using the Fortify::authenticateUsing
method and it should be called from the boot
method.您可以使用Fortify::authenticateUsing
方法在App > Providers
自定义FortifyServiceProvider
,它应该从boot
方法调用。
public function boot()
{
Fortify::authenticateUsing(function (Request $request) {
$user = User::where('email', $request->email)->first();
if ($user &&
Hash::check($request->password, $user->password)) {
return $user;
} else {
$request->session()->flash('message', 'Invalid!');
return false;
}
});
}
go to your vendor folder vendor\\laravel\\fortify\\src\\Http\\Requests\\LoginRequest.php in the login request class create a function as you would if you created a request class.转到登录请求类中的供应商文件夹 vendor\\laravel\\fortify\\src\\Http\\Requests\\LoginRequest.php 创建一个函数,就像创建请求类一样。 public function messages(){ customize the message eg 'email.required' => 'email required'} public function messages(){ 自定义消息,例如 'email.required' => 'email required'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.