使用登录表单中的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');
}
我收到了Flash消息,但是任何字段的验证错误都丢失了。
如果我尝试制作:
$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 !');
...
和模板资源/视图/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>
哪种方法有效?
谢谢!
答案 0 :(得分:1)
Livewire的优点在于您不必重定向以显示消息,可以通过在组件上设置属性并有条件地在视图中呈现消息来显示消息。在这种特殊情况下,已经很容易获得逻辑,您只需要检查验证所暴露的错误对象即可。
您认为,您所要做的就是选中@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将为您完成大部分工作。您无需返回任何重定向,也无需使用session()->flash()
。会话状态不会闪烁,因为您不执行新的页面加载。
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
答案 1 :(得分:0)
在渲染方法之前,您可以检查errorBag是否包含项:
public function render()
{
if(count($this->getErrorBag()->all()) > 0){
$this->emit('error:example');
}
return view('livewire-component-view');
}