如题,Laravel Filament 自定义页面中包含表单,提交时报错:No property found for validation。
错误代码如下:
<?php
namespace App\Filament\Management\Pages;
use App\Settings\BedroomSettings as Settings;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
class BedroomSettings extends Page implements HasForms
{
use InteractsWithForms;
protected static ?string $navigationIcon = 'heroicon-o-document-text';
protected static ?string $navigationLabel = '设置';
protected static ?string $title = '设置';
protected static string $view = 'filament.management.pages.bedroom-settings';
public function form(Form $form): Form
{
return $form
->schema([
TextInput::make('number')
->label(__('settings.bedroom.number'))
->required()
->live(),
TextInput::make('price')
->label(__('settings.bedroom.price'))
->required()
->live(),
DateTimePicker::make('open_time')
->label(__('settings.bedroom.open_time'))
->required()
->live(),
DateTimePicker::make('close_time')
->label(__('settings.bedroom.close_time'))
->required()
->live()
])
->columns(2)
->statePath('data');
}
public function mount(): void
{
$settings = app(Settings::class);
$this->form->fill($settings->toArray());
}
public function update(): void
{
$settings = new Settings();
foreach ($this->form->getState() as $key => $value){
$settings->$key = $value;
}
$settings->save();
Notification::make()
->title('设置完成')
->success()
->send();
}
}
红色代码为出错位置,产生错误的原因是我通过statePath('data')
将表单数据保存到$data
属性中,但是我的类中却没定义$data
属性,在类开始部分添加下面代码即可:
public $data = [];
一条评论
这篇文章写得深入浅出,让我这个小白也看懂了!