Filament FileUpload maxSize 被忽略

问题描述 投票:0回答:1

我有火线组件,其中有这种灯丝形式


class Categories extends Component implements HasForms, HasTable
{
use InteractsWithForms;
use InteractsWithTable;

public $customFields;

public function mount()
{
    $this->customFields = CustomField::where('field_placement', FieldPlacementEnum::Categories->value)
        ->where(function ($query) {
            $query->whereNull('team_id')
                ->orWhere('team_id', auth()->user()->currentTeam->id);
        })->get();
}

public function render()
{
    return view('livewire.categories');
}

public function table(Table $table): Table
{
    $query = Category::query()->with('products')->where('team_id', Auth::user()->currentTeam->id);

    return $table
        ->query($query)
        ->heading('Categories')
        ->paginated([10, 25, 50, 100])
        ->columns([
            TextColumn::make('name')->sortable()->searchable()->toggleable(),
            TextColumn::make('email')->sortable()->searchable()->toggleable(),
            TextColumn::make('phone')->sortable()->searchable()->toggleable(),
            TextColumn::make('website')->sortable()->searchable()->toggleable(),
            TextColumn::make('products_count')->counts('products')->label('Products')
                ->sortable()->toggleable(),
            TextColumn::make('country')->sortable()->searchable()->toggleable(),
            TextColumn::make('city')->sortable()->searchable()->toggleable(),
        ])->defaultSort('created_at', 'desc')
        ->headerActions([
            CreateAction::make()
                ->slideOver()
                ->label('Add Category')
                ->model(Category::class)
                ->createAnother(false)
                ->visible(auth()->user()->can('add-category'))
                ->form([
                    TextInput::make('name')->label('Category Name')->required()->string()->minLength(1)->maxLength(255),
                    FileUpload::make('logo')->disk('team_images')
                        ->nullable()->image()->imageEditor()->imageEditorAspectRatios([
                            null,
                            '16:9',
                            '4:3',
                            '1:1',
                        ])->maxSize(1024),
                    TextInput::make('email')->label('Email')->required()->email()->maxLength(255),
                    TextInput::make('phone')->label('Phone No'),
                    TextInput::make('website')->label('Website'),
                    TextInput::make('country')->label('Country')->required()->string()->minLength(1)->maxLength(255),
                    TextInput::make('city')->label('City')->required()->string()->minLength(1)->maxLength(255),
                    Textarea::make('ai_description')->label('AI Description')->nullable()->string()->minLength(1)->maxLength(1024),

                    Grid::make()->columns(1)->visible($this->customFields->count() > 0)
                        ->schema(getDynamicSchema($this->customFields)),

                ])
                ->action(function (array $data) {
                    $data['team_id'] = auth()->user()->currentTeam->id;
                    $data['created_by'] = auth()->user()->id;

                    Category::create($data);
                    Notification::make()
                        ->success()
                        ->color('success')
                        ->title('Category added')
                        ->body('The Category has been added successfully.')
                        ->send();
                }),
        ])
        ->actions([
            EditAction::make()
                ->slideOver()
                ->visible(auth()->user()->can('edit-category'))
                ->model(Category::class)->form([
                    TextInput::make('name')->label('Category Name')->required()->string()->minLength(1)->maxLength(255),
                    FileUpload::make('logo')->disk('team_images')
                        ->nullable()->image()->imageEditor()->imageEditorAspectRatios([
                            null,
                            '16:9',
                            '4:3',
                            '1:1',
                        ])->maxSize(5120),
                    TextInput::make('email')->label('Email')->required()->email()->maxLength(255),
                    TextInput::make('phone'),
                    TextInput::make('website'),
                    TextInput::make('country')->label('Country')->required()->string()->minLength(1)->maxLength(255),
                    TextInput::make('city')->label('City')->required()->string()->minLength(1)->maxLength(255),
                    Textarea::make('ai_description')->label('AI Description')->nullable()->string()->minLength(1)->maxLength(1024),

                    Grid::make()->columns(1)->visible($this->customFields->count() > 0)
                        ->schema(getDynamicSchema($this->customFields)),
                ])
                ->action(function (Category $record, array $data) {
                    $record->update($data);
                    Notification::make()
                        ->success()
                        ->color('success')
                        ->title('Category Updated')
                        ->body('The Category has been updated successfully.')
                        ->send();
                }),
            DeleteAction::make()->visible(auth()->user()->can('delete-category'))

        ]);
}

现在你一定想知道这是什么

 Grid::make()->columns(1)->visible($this->customFields->count() > 0) ->schema(getDynamicSchema($this->customFields)),


我有一个名为 getDynamicSchema 的辅助函数,它可以像这样动态加载自定义输入


function getDynamicSchema($customFields): array
{
// Build the form schema dynamically based on the custom fields
return $customFields->map(function ($field) {
$schema = null;

    // Handle different input types
    switch ($field->field_type) {
        case 'text':
            $schema = TextInput::make("custom_inputs.{$field->field_name}")
                ->label($field->field_label)
                ->hint($field->field_description)
                ->placeholder($field->field_placeholder)
                ->required($field->field_required);

            // Conditionally apply minLength and maxLength only if not null
            if (!is_null($field->field_min)) {
                $schema->minLength($field->field_min);
            }
            if (!is_null($field->field_max)) {
                $schema->maxLength($field->field_max);
            }

            // Add regex validation if applicable
            if ($field->field_regex) {
                $schema->rule('regex:' . $field->field_regex);
            }
            break;

        case 'number':
            $schema = TextInput::make("custom_inputs.{$field->field_name}")
                ->label($field->field_label)
                ->hint($field->field_description)
                ->placeholder($field->field_placeholder)
                ->numeric() // Ensure input is a number
                ->required($field->field_required);

            // Conditionally apply minValue and maxValue only if not null
            if (!is_null($field->field_min)) {
                $schema->minValue($field->field_min);
            }
            if (!is_null($field->field_max)) {
                $schema->maxValue($field->field_max);
            }
            if ($field->field_regex) {
                $schema->rule('regex:' . $field->field_regex);
            }
            break;


        case 'email':
            $schema = TextInput::make("custom_inputs.{$field->field_name}")
                ->email()
                ->label($field->field_label)
                ->hint($field->field_description)
                ->placeholder($field->field_placeholder)
                ->required($field->field_required);

            // Conditionally apply minValue and maxValue only if not null
            if (!is_null($field->field_min)) {
                $schema->minLength($field->field_min);
            }
            if (!is_null($field->field_max)) {
                $schema->maxLength($field->field_max);
            }
            if ($field->field_regex) {
                $schema->rule('regex:' . $field->field_regex);
            }
            break;

        case 'date':
            $schema = DatePicker::make("custom_inputs.{$field->field_name}")
                ->label($field->field_label)
                ->hint($field->field_description)
                ->placeholder($field->field_placeholder)
                ->required($field->field_required);

            if ($field->field_regex) {
                $schema->rule('regex:' . $field->field_regex);
            }
            break;

        case 'time':
            $schema = TimePicker::make("custom_inputs.{$field->field_name}")
                ->label($field->field_label)
                ->hint($field->field_description)
                ->placeholder($field->field_placeholder)
                ->required($field->field_required);

            if ($field->field_regex) {
                $schema->rule('regex:' . $field->field_regex);
            }
            break;

        case 'datetime':
            $schema = DateTimePicker::make("custom_inputs.{$field->field_name}")
                ->label($field->field_label)
                ->hint($field->field_description)
                ->placeholder($field->field_placeholder)
                ->required($field->field_required);

            if ($field->field_regex) {
                $schema->rule('regex:' . $field->field_regex);
            }
            break;

        case 'textarea':
            $schema = Textarea::make("custom_inputs.{$field->field_name}")
                ->label($field->field_label)
                ->hint($field->field_description)
                ->placeholder($field->field_placeholder)
                ->required($field->field_required);

            if (!is_null($field->field_min)) {
                $schema->minLength($field->field_min);
            }
            if (!is_null($field->field_max)) {
                $schema->maxLength($field->field_max);
            }
            if ($field->field_regex) {
                $schema->rule('regex:' . $field->field_regex);
            }
            break;



        case 'select':
            $schema = Select::make("custom_inputs.{$field->field_name}")
                ->label($field->field_label)
                ->hint($field->field_description)
                ->options($field->field_options) // Assuming the field has an options array
                ->required($field->field_required);
            break;


        case 'multi-select':
            $schema = Select::make("custom_inputs.{$field->field_name}")
                ->multiple()
                ->label($field->field_label)
                ->hint($field->field_description)
                ->options($field->field_options) // Assuming the field has an options array
                ->required($field->field_required);

            if ($field->field_min) {
                $schema->minItems($field->field_min);
            }
            if ($field->field_max) {
                $schema->maxItems($field->field_max);
            }
            break;

        case 'toggle':
            $schema = Toggle::make("custom_inputs.{$field->field_name}")
                ->label($field->field_label)
                ->hint($field->field_description);
            break;

        case 'radio':
            $schema = Radio::make("custom_inputs.{$field->field_name}")
                ->label($field->field_label)
                ->hint($field->field_description)
                ->default('Yes')
                ->options([
                    'Yes' => 'Yes',
                    'No' => 'No'
                ]);

            break;

        case 'radio-custom':
            $schema = Radio::make("custom_inputs.{$field->field_name}")
                ->label($field->field_label)
                ->hint($field->field_description)
                ->default(array_key_first($field->field_options))
                ->options($field->field_options);

            break;

        case 'checkbox':
            $schema = Checkbox::make("custom_inputs.{$field->field_name}")
                ->label($field->field_label)
                ->hint($field->field_description);
            break;

        case 'checkbox-custom':
            $schema = CheckboxList::make("custom_inputs.{$field->field_name}")
                ->label($field->field_label)
                ->hint($field->field_description)
                ->options($field->field_options);
            break;

        case 'file':
            $schema = FileUpload::make("custom_inputs.{$field->field_name}")
                ->disk('team_images')
                ->label($field->field_label)
                ->hint($field->field_description)
                ->required($field->field_required);

            if (!is_null($field->field_max)) {
                $schema->maxSize($field->field_max);
            }
            break;

        default:
            $schema = null;
            break;
    }

    return $schema;
})->toArray();
}


所以问题是,如果直接在 createAction 表单中而不是在网格中动态创建表单输入,我的表单会正确验证 maxSize(1024),这意味着我可以上传最多 8mb 或更高,但我的动态 field_max 的值 = 1024

laravel laravel-livewire laravel-filament filepond filamentphp
1个回答
0
投票

过了很长一段时间我自己找到了解决方案

而不是这个

FileUpload::make("custom_inputs.{$field->field_name}")

这样做

FileUpload::make($field->field_name)

还要更新网格,在最后添加以下内容

->statePath('custom_inputs')

这是更新后的样子

Grid::make()->columns(1)->visible($this->customFields->count() > 0)
                            ->schema(getDynamicSchema($this->customFields))->statePath('custom_inputs')
© www.soinside.com 2019 - 2024. All rights reserved.