1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Filament FileUpload maxSize is ignored

Discussão em 'Outras Linguagens' iniciado por Rana Abdul Rauf, Novembro 8, 2024 às 10:32.

  1. i have livewire component where i have this filament form


    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'))

    ]);
    }



    now you must be wondering what is this

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




    i have helper function called getDynamicSchema which dynamically load custom inputs like this


    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();
    }




    So the problem is that my form correctly validate the maxSize(1024) if this is inside createAction form directly but not with dynamically created form inputs in the grid, which means i can upload upto 8mb or higher but my dynamic field_max has value = 1024

    Continue reading...

Compartilhe esta Página