英文:
Unable to display validation errors in Laravel
问题
我尝试显示使用验证创建的表单的错误,但当出现错误时,显示上没有任何内容。
我尝试了@dump(Session::has('status')) @dump($errors->any())
,但每次在我的视图中这两个值都保持为false。
这是我的登录函数。
public function login(Request $request)
{
$credentials = $request->validate([
'email' => 'email|max:90|required',
'password' => 'required|min:6',
]);
if (Auth::attempt($credentials, true)) {
$accessToken = auth()->user()->createToken('authToken')->plainTextToken;
return redirect('/home')->withCookie('auth_token', $accessToken);
} else {
$notification = 'L\'email ou le mot de passe est incorrecte';
return back()->with('status', $notification);
}
}
我的表单
<form action="{{ url('/authentification') }}" class="request-form ftco-animate bg-primary" method="POST">@csrf
<h2>Connexion</h2>
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if (Session::has('status'))
<div class="alert alert-danger">
{{ Session::get('status') }}
</div>
@endif
<div class="form-group">
<label for="" class="label">Email</label>
<input type="text" class="form-control" placeholder="Email" name="email">
</div>
<div class="form-group mr-2">
<label for="" class="label">Mot de passe</label>
<input type="password" class="form-control" placeholder="**********" name="password">
</div>
<div class="form-group">
<input type="submit" value="Connexion" class="btn btn-secondary py-3 px-4">
<a href="{{ url('/register') }}" class="text-center" style="color: white;">Pas encore de compte ?</a>
</div>
</form>
提前感谢您的帮助。
英文:
I try to display the errors of my form that I create with validation but nothing shows on the display when I have an error
I tried @dump(Session::has('status')) @dump($errors-\>any())
, but each time the 2 values remain false in my view.
Here is my login function.
public function login(Request $request)
{
$credentials = $request->validate([
'email' => 'email|max:90|required',
'password' => 'required|min:6',
]);
if (Auth::attempt($credentials, true)) {
$accessToken = auth()->user()->createToken('authToken')->plainTextToken;
return redirect('/home')->withCookie('auth_token', $accessToken);
} else {
$notification = 'L\'email ou le mot de passe est incorrecte';
return back()->with('status', $notification);
}
}
My form
<form action="{{ url('/authentification') }}" class="request-form ftco-animate bg-primary"method="POST"> @csrf
<h2>Connexion</h2>
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if (Session::has('status'))
<div class="alert alert-danger">
{{ Session::get('status') }}
</div>
@endif
<div class="form-group">
<label for="" class="label">Email</label>
<input type="text" class="form-control" placeholder="Email" name="email">
</div>
<div class="form-group mr-2">
<label for="" class="label">Mot de passe</label>
<input type="password" class="form-control" placeholder="**********" name="password">
</div>
<div class="form-group">
<input type="submit" value="Connexion" class="btn btn-secondary py-3 px-4">
<a href="{{ url('/register') }}" class="text-center" style="color: white;">Pas encore de compte ?</a>
</div>
</form>
I thank you in advance for your help
答案1
得分: 0
当您使用with()
方法从控制器返回状态时,它将变量返回,而不是作为会话键。因此,在Blade文件中:
@if ($status ?? false)
<div class="alert alert-danger">
{{ $status }}
</div>
@endif
在您的控制器中:
return back()->with('status', $notification);
或者,如果您想使用会话:
在控制器中:
return back()->withErrors('status', $notification);
在Blade代码中保持不变。
英文:
When you return status from controller with with()
method it returns variable not as session key so in blade file:
@if ($status ?? false)
<div class="alert alert-danger">
{{ $status }}
</div>
@endif
and in your controller
return back()->with('status', $notification);
OR if you want to use session instead :
in controller:
return back()->withErrors('status', $notification);
and in blade code will remain the same.
references: https://laracasts.com/discuss/channels/laravel/return-success-message
答案2
得分: 0
安装 Blade 时,未从 Laravel Breeze 安装 auth-validation-errors-blade.php 文件。
英文:
When installed blade,auth-validation-errors-blade.php file did not installed from laravel breeze
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论