如何在 laravel-livewire 中设置带有验证错误的闪现消息
How in laravel-livewire set flash message with validation erros
在登录表单中使用 laravel 7 /livewire 1.3 应用程序时,我收到无效表单错误代码:
public function submit()
{
$loginRules= User::getUserValidationRulesArray();
$this->validate($loginRules);
并在任何字段附近显示错误消息
我想在登录失败时添加闪信和阅读
https://laravel.com/docs/7.x/validation
我试着做 :
$request = request();
$loginRules= User::getUserValidationRulesArray('login');
$validator = Validator::make($request->all(), $loginRules);
if ($validator->fails()) {
session()->flash('danger_message', 'Check your credentials !');
return redirect()->to('/login');
}
我收到了闪现消息,但任何字段的验证错误都丢失了。
如果我尝试制作:
$request = request();
$loginRules= User::getUserValidationRulesArray('login');
$validator = Validator::make($request->all(), $loginRules);
if ($validator->fails()) {
session()->flash('danger_message', 'Check your credentials !');
return redirect('/login')
->withErrors($validator)
->withInput();
}
我得到了错误:
Method Livewire\Redirector::withErrors does not exist.
在 routes/web.php 我有 :
Route::livewire('/login', 'login')->name('login');
已修改:
在组件 app/Http/Livewire/Login.php 中:
<?php
namespace App\Http\Livewire;
use App\User;
use Illuminate\Support\Facades\Validator;
use Livewire\Component;
use Auth;
use DB;
use App\Config;
use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
class Login extends Component
{
public $form= [
'email'=>'admin@mail.com',
'password'=> '111111',
];
private $view_name= 'livewire.auth.login';
public function submit()
{
$request = request();
$loginRules= User::getUserValidationRulesArray('login');
$validator = Validator::make($request->all(), $loginRules);
if ($validator->fails()) {
session()->flash('danger_message', 'Check your credentials !');
return;
// return redirect()->to('/login');
}
$user = Sentinel::findByCredentials(['email' => $this->form['email']]);
if (empty($user)) {
session()->flash('danger_message', 'User "' . $this->form['email'] . '" not found !');
...
和模板 resources/views/livewire/auth/login.blade.php :
<article >
@include('livewire.common.alert_messages')
<form class="form-login" wire:submit.prevent="submit">
<div class="card">
@if ($errors->any())
Check your login credentials
@endif
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
<div class="card-body card-block">
<h3 class="card-header">
<span class="spinner-border" role="status" wire:loading>
<span class="sr-only">Loading...</span>
</span>
Login
</h3>
<h4 class="card-subtitle">Use your credentials</h4>
<dl> <!-- email FIELD DEFINITION -->
<dt>
<label class="col-form-label" for="email">Email:<span class="required"> * </span></label>
</dt>
<dd>
<input
wire:model.lazy="form.email"
name="email"
id="email"
class="form-control"
placeholder="Your email address"
autocomplete=off
>
@error('form.email')
<div class="validation_error">{{ clearValidationError($message,['form.'=>'']) }}</div> @enderror
</dd>
</dl> <!-- <dt> email FIELD DEFINITION -->
<dl> <!-- password FIELD DEFINITION -->
<dt>
<label class="col-form-label" for="password">Password:<span class="required"> * </span></label>
</dt>
<dd>
<input type="password"
wire:model.lazy="form.password"
id="password"
name="password"
class="form-control"
placeholder="Your password"
autocomplete=off
>
@error('form.password')
<div class="validation_error">{{ clearValidationError($message,['form.'=>'']) }}</div> @enderror
</dd>
</dl> <!-- <dl> password FIELD DEFINITION -->
</div> <!-- <div class="card-body card-block"> -->
<section class="card-footer row_content_right_aligned">
<button type="reset" class="btn btn-secondary btn-sm m-2">
Reset
</button>
<button type="submit" class="btn btn-primary btn-sm m-2 ml-4 mr-4 action_link">
Submit
</button>
</section>
</div> <!-- <div class="card"> -->
</form>
</article>
哪种方式有效?
提前致谢!
Livewire 的美妙之处在于您不一定需要重定向来显示消息,您可以通过在组件上设置属性并有条件地在视图中呈现它们来显示消息。在这种特殊情况下,已经有现成的逻辑可用,您只需检查验证公开的 errors-object。
在您看来,您所要做的就是检查 @if ($errors->any())
- 如果是,则显示您的消息。这是 Livewire 实现的 Laravel 功能。当任何验证失败时,都会抛出并拦截异常,并且 $errors
变量会暴露给您的视图。这意味着无论何时执行 $this->validate()
并且验证失败,您都可以访问 $errors
.
中的错误
<div>
@if ($errors->any())
Check your login credentials
@endif
<form wire:submit.prevent="submit">
<input type="text" wire:model="email">
@error('email') <span class="error">{{ $message }}</span> @enderror
<input type="password" wire:model="password">
@error('password') <span class="error">{{ $message }}</span> @enderror
<button type="submit">Submit</button>
</form>
</div>
使用 $rules
属性声明规则,使用 $this->validate()
验证这些规则,Livewire 将为您完成大部分工作。您不需要 return 任何重定向,或使用 session()->flash()
。 session-state不会闪烁,因为你没有执行新的页面加载。
class Login extends Component
{
public $form = [
'email' => 'admin@mail.com',
'password' => '111111',
];
protected $rules;
private $view_name = 'livewire.auth.login';
public function submit()
{
$this->rules = User::getUserValidationRulesArray('login');
$this->validate();
// No need to do any more checks, $errors will now be updated in your view as the exception is thrown
// Proceed with submitting the form
在 render 方法之前你可以检查 errorBag 是否有项目:
public function render()
{
if(count($this->getErrorBag()->all()) > 0){
$this->emit('error:example');
}
return view('livewire-component-view');
}
在登录表单中使用 laravel 7 /livewire 1.3 应用程序时,我收到无效表单错误代码:
public function submit()
{
$loginRules= User::getUserValidationRulesArray();
$this->validate($loginRules);
并在任何字段附近显示错误消息
我想在登录失败时添加闪信和阅读 https://laravel.com/docs/7.x/validation
我试着做 :
$request = request();
$loginRules= User::getUserValidationRulesArray('login');
$validator = Validator::make($request->all(), $loginRules);
if ($validator->fails()) {
session()->flash('danger_message', 'Check your credentials !');
return redirect()->to('/login');
}
我收到了闪现消息,但任何字段的验证错误都丢失了。
如果我尝试制作:
$request = request();
$loginRules= User::getUserValidationRulesArray('login');
$validator = Validator::make($request->all(), $loginRules);
if ($validator->fails()) {
session()->flash('danger_message', 'Check your credentials !');
return redirect('/login')
->withErrors($validator)
->withInput();
}
我得到了错误:
Method Livewire\Redirector::withErrors does not exist.
在 routes/web.php 我有 :
Route::livewire('/login', 'login')->name('login');
已修改: 在组件 app/Http/Livewire/Login.php 中:
<?php
namespace App\Http\Livewire;
use App\User;
use Illuminate\Support\Facades\Validator;
use Livewire\Component;
use Auth;
use DB;
use App\Config;
use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
class Login extends Component
{
public $form= [
'email'=>'admin@mail.com',
'password'=> '111111',
];
private $view_name= 'livewire.auth.login';
public function submit()
{
$request = request();
$loginRules= User::getUserValidationRulesArray('login');
$validator = Validator::make($request->all(), $loginRules);
if ($validator->fails()) {
session()->flash('danger_message', 'Check your credentials !');
return;
// return redirect()->to('/login');
}
$user = Sentinel::findByCredentials(['email' => $this->form['email']]);
if (empty($user)) {
session()->flash('danger_message', 'User "' . $this->form['email'] . '" not found !');
...
和模板 resources/views/livewire/auth/login.blade.php :
<article >
@include('livewire.common.alert_messages')
<form class="form-login" wire:submit.prevent="submit">
<div class="card">
@if ($errors->any())
Check your login credentials
@endif
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
<div class="card-body card-block">
<h3 class="card-header">
<span class="spinner-border" role="status" wire:loading>
<span class="sr-only">Loading...</span>
</span>
Login
</h3>
<h4 class="card-subtitle">Use your credentials</h4>
<dl> <!-- email FIELD DEFINITION -->
<dt>
<label class="col-form-label" for="email">Email:<span class="required"> * </span></label>
</dt>
<dd>
<input
wire:model.lazy="form.email"
name="email"
id="email"
class="form-control"
placeholder="Your email address"
autocomplete=off
>
@error('form.email')
<div class="validation_error">{{ clearValidationError($message,['form.'=>'']) }}</div> @enderror
</dd>
</dl> <!-- <dt> email FIELD DEFINITION -->
<dl> <!-- password FIELD DEFINITION -->
<dt>
<label class="col-form-label" for="password">Password:<span class="required"> * </span></label>
</dt>
<dd>
<input type="password"
wire:model.lazy="form.password"
id="password"
name="password"
class="form-control"
placeholder="Your password"
autocomplete=off
>
@error('form.password')
<div class="validation_error">{{ clearValidationError($message,['form.'=>'']) }}</div> @enderror
</dd>
</dl> <!-- <dl> password FIELD DEFINITION -->
</div> <!-- <div class="card-body card-block"> -->
<section class="card-footer row_content_right_aligned">
<button type="reset" class="btn btn-secondary btn-sm m-2">
Reset
</button>
<button type="submit" class="btn btn-primary btn-sm m-2 ml-4 mr-4 action_link">
Submit
</button>
</section>
</div> <!-- <div class="card"> -->
</form>
</article>
哪种方式有效?
提前致谢!
Livewire 的美妙之处在于您不一定需要重定向来显示消息,您可以通过在组件上设置属性并有条件地在视图中呈现它们来显示消息。在这种特殊情况下,已经有现成的逻辑可用,您只需检查验证公开的 errors-object。
在您看来,您所要做的就是检查 @if ($errors->any())
- 如果是,则显示您的消息。这是 Livewire 实现的 Laravel 功能。当任何验证失败时,都会抛出并拦截异常,并且 $errors
变量会暴露给您的视图。这意味着无论何时执行 $this->validate()
并且验证失败,您都可以访问 $errors
.
<div>
@if ($errors->any())
Check your login credentials
@endif
<form wire:submit.prevent="submit">
<input type="text" wire:model="email">
@error('email') <span class="error">{{ $message }}</span> @enderror
<input type="password" wire:model="password">
@error('password') <span class="error">{{ $message }}</span> @enderror
<button type="submit">Submit</button>
</form>
</div>
使用 $rules
属性声明规则,使用 $this->validate()
验证这些规则,Livewire 将为您完成大部分工作。您不需要 return 任何重定向,或使用 session()->flash()
。 session-state不会闪烁,因为你没有执行新的页面加载。
class Login extends Component
{
public $form = [
'email' => 'admin@mail.com',
'password' => '111111',
];
protected $rules;
private $view_name = 'livewire.auth.login';
public function submit()
{
$this->rules = User::getUserValidationRulesArray('login');
$this->validate();
// No need to do any more checks, $errors will now be updated in your view as the exception is thrown
// Proceed with submitting the form
在 render 方法之前你可以检查 errorBag 是否有项目:
public function render()
{
if(count($this->getErrorBag()->all()) > 0){
$this->emit('error:example');
}
return view('livewire-component-view');
}