[英]Method Livewire\Redirector::withInput does not exist. in laravel
我正在使用火線,當我嘗試驗證表單時,它說我正在使用火線,當我嘗試驗證表單時,它說我正在使用火線,當我嘗試驗證表單時,它說我正在使用火線當我嘗試驗證表單時,它說
Method Livewire\Redirector::withInput does not exist.
這是我的代碼
Posts.php
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Post;
class Posts extends Component
{
public $title;
public $content;
public function hydrate(){
$this->validate([
'title' => 'required',
'content' => 'required'
]);
}
public function save(){
$data = [
'title' => $this->title,
'content' => $this->content,
'user_id' => Auth()->user()->id
];
Post::create($data);
$this->cleanVars();
}
private function cleanVars(){
$this->title = null;
$this->content = null;
}
public function render()
{
return view('livewire.posts');
}
}
直播視圖
<div>
<label>Title</label>
<input wire:model="title" type="text" class="form-control" />
@error('title')
<p class="text-danger">{{ $message }}</p>
@enderror
<label>Content</label>
<textarea wire:model="content" type="text" class="form-control"></textarea>
@error('content')
<p class="text-danger">{{ $message }}</p>
@enderror
<br />
<button wire:click="save" class="btn btn-primary">Save</button>
</div>
我也把這個視圖放在home.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Dashboard') }}</div>
<div class="card-body">
@livewire('posts')
</div>
</div>
</div>
</div>
</div>
@endsection
你真的需要修復這個問題的標題,重復同樣的問題,你在社區中遺漏了一些東西。 我在你的代碼中看到了這個,我不喜歡這種用法
public function hydrate(){
$this->validate([
'title' => 'required',
'content' => 'required'
]);
}
我的意思是,這種在每次補水時運行的驗證並不是一個好的方法。 相反,聲明規則
protected $rules = [// rules here];
//or
public function rules()
{
return [
//rules here
];
}
然后您可以驗證條目,例如使用 updated() 中的 validateOnly 方法進行實時驗證
public function updated($propertyName)
{
$this->validateOnly($propertyName, $this->rules());
}
或者只是在 save 方法中使用它
public function save()
{
$this->validate(); // in the protected $rules property
// or
Post::create($this->validate());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.