You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Filament Wizard报错:未找到验证属性[file_name]

Filament Wizard切换步骤时出现“No property found for validation: [file_name]”错误

问题描述

在使用Laravel Filament管理面板时,动态生成Wizard步骤时遇到问题:切换向导步骤时触发错误 No property found for validation: [file_name]。

配置背景

  • 存在CategoryApplication模型,与CategoryStep关联
  • 在CustomCreateCategoryApplicationSteps页面中,基于关联的CategoryStep动态生成向导步骤,每个步骤包含一个输入文件名的TextInput组件

原代码

<?php

namespace App\Filament\Resources\CategoryApplicationStepResource\Pages;

use App\Filament\Resources\CategoryApplicationStepResource;
use App\Models\CategoryApplication;
use App\Models\CategoryApplicationStep;
use App\Models\CategoryStep;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Pages\Page;
use Filament\Forms\Form;
use Filament\Forms\Components\Wizard;
use Filament\Notifications\Notification;
use Filament\Actions\Action;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\FileUpload;

class CustomCreateCategoryApplicationSteps extends Page
{
    protected static string $resource = CategoryApplicationStepResource::class;

    protected static string $view = 'filament.resources.category-application-step-resource.pages.custom-create-category-application-steps';

    protected static ?string $label = 'Apply Category';

    protected static ?string $title = 'Apply Category';

    public $categorySteps;

    public $originalCategorySteps;

    public function mount($record): void
    {
        $categoryApplication = CategoryApplication::findOrFail($record);
        $category = $categoryApplication->category;
        $this->categorySteps = $category->categorySteps;

        //dd($this->categorySteps);
        //$this->form->fill();
    }


    public function form(Form $form): Form
    {
        $wizardSteps = [];

        foreach ($this->categorySteps as $categoryStep) {
            $wizardSteps[] = Wizard\Step::make($categoryStep->step_title)
                ->schema([
                    TextInput::make('file_name')
                        ->id("category-step".$categoryStep->id)
                        ->label('Upload File')
                        ->required(),
                ]);
        }

        return $form
            ->schema([Wizard::make($wizardSteps)]);
    }

    public function getFormActions()
    {
        return [
            Action::make('save')
                ->label(__('filament-panels::resources/pages/create-record.form.actions.create.label'))
                ->submit('save')
        ];
    }

    public function save(): void
    {
        $data = $this->form->getState();

        foreach ($data['categorySteps'] as $step) {
            CategoryApplicationStep::create([
                'category_step_id' => $step['id'],
                'category_application_id' => $this->record,
                'file_name' => $step['file_name'],
            ]);
        }

        Notification::make()
            ->success()
            ->title("Success!")
            ->send();
    }
}

问题原因

所有动态生成的TextInput都使用了相同的file_name字段名,Filament的表单状态管理无法区分不同步骤的同名字段,切换步骤时验证系统找不到对应属性。同时,save方法中尝试读取data['categorySteps'],但当前表单结构并没有将步骤数据组织到这个键名下。

解决方案

修改要点

  1. 给每个TextInput设置唯一字段名,结合CategoryStep的ID避免冲突
  2. 调整save方法,适配新的字段结构来获取每个步骤的提交数据

修改后的完整代码

<?php

namespace App\Filament\Resources\CategoryApplicationStepResource\Pages;

use App\Filament\Resources\CategoryApplicationStepResource;
use App\Models\CategoryApplication;
use App\Models\CategoryApplicationStep;
use App\Models\CategoryStep;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Pages\Page;
use Filament\Forms\Form;
use Filament\Forms\Components\Wizard;
use Filament\Notifications\Notification;
use Filament\Actions\Action;

class CustomCreateCategoryApplicationSteps extends Page
{
    protected static string $resource = CategoryApplicationStepResource::class;
    protected static string $view = 'filament.resources.category-application-step-resource.pages.custom-create-category-application-steps';
    protected static ?string $label = 'Apply Category';
    protected static ?string $title = 'Apply Category';

    public $categorySteps;

    public function mount($record): void
    {
        $categoryApplication = CategoryApplication::findOrFail($record);
        $this->categorySteps = $categoryApplication->category->categorySteps;
    }

    public function form(Form $form): Form
    {
        $wizardSteps = [];

        foreach ($this->categorySteps as $categoryStep) {
            $wizardSteps[] = Wizard\Step::make($categoryStep->step_title)
                ->schema([
                    TextInput::make("file_name_{$categoryStep->id}")
                        ->id("category-step{$categoryStep->id}")
                        ->label('Upload File')
                        ->required(),
                ]);
        }

        return $form->schema([Wizard::make($wizardSteps)]);
    }

    public function getFormActions()
    {
        return [
            Action::make('save')
                ->label(__('filament-panels::resources/pages/create-record.form.actions.create.label'))
                ->submit('save')
        ];
    }

    public function save(): void
    {
        $data = $this->form->getState();

        foreach ($this->categorySteps as $categoryStep) {
            $fileNameKey = "file_name_{$categoryStep->id}";
            
            CategoryApplicationStep::create([
                'category_step_id' => $categoryStep->id,
                'category_application_id' => $this->record,
                'file_name' => $data[$fileNameKey],
            ]);
        }

        Notification::make()
            ->success()
            ->title("Success!")
            ->send();
    }
}

说明

  • 每个TextInput的字段名改为file_name_{$categoryStep->id},确保唯一性,解决验证时的属性找不到问题
  • save方法中通过遍历categorySteps,拼接对应的字段名来获取每个步骤的文件名,正确创建关联记录

内容的提问来源于stack exchange,提问作者Kaan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 16:47:10