Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileUpload dimensions() validation #11904

Open
valpuia604 opened this issue Mar 18, 2024 · 10 comments · Fixed by #11907
Open

FileUpload dimensions() validation #11904

valpuia604 opened this issue Mar 18, 2024 · 10 comments · Fixed by #11907
Labels
enhancement New feature or request low priority
Milestone

Comments

@valpuia604
Copy link
Contributor

valpuia604 commented Mar 18, 2024

Package

filament/filament

Package Version

v3.2.51

Laravel Version

v11.0.7

Livewire Version

v3.4.9

PHP Version

v8.3.4

Problem description

When rendering dynamic components based on different table/model, the rules() or rule() is not working in FileUpload, but If I used "predefine methods (?)" like ->required() it's working tho.. This same behaviour is working fine for TextInput, but not in FileUpload

Expected behavior

Expected that rules() or rule() working in dynamic components for FileUpload

Steps to reproduce

Install, check Post resource and you can see the rules() is working in TextInput but not in FileUpload

Reproduction repository

https://github.com/valpuia604/dynamic-component

Relevant log output

No response

Update:

I've check more and more and I think that ->rules([]) is not working in FileUpload whether it's dynamic or not

Donate 💰 to fund this issue

  • You can donate funding to this issue. We receive the money once the issue is completed & confirmed by you.
  • 100% of the funding will be distributed between the Filament core team to run all aspects of the project.
  • Thank you in advance for helping us make maintenance sustainable!
Fund with Polar
@valpuia604 valpuia604 added bug Something isn't working low priority unconfirmed labels Mar 18, 2024
@github-project-automation github-project-automation bot moved this to Todo in Roadmap Mar 18, 2024
@valpuia604 valpuia604 changed the title rules() with dynamic form components FileUpload rules() with dynamic form components Mar 18, 2024
@valpuia604
Copy link
Contributor Author

If I put like this (without dynamic) then it's working

Forms\Components\FileUpload::make('img')
    ->label('img')
    ->image()
    ->required() // this is working
    ->rules([
        // 'required',
        'dimensions:width=100,height=200',
        // Rule::dimensions()->width(10)->height(50),
    ]),

But In dynamic it's not

Forms\Components\FileUpload::make('trans.'.$language->id.'.image')
    ->label('image')
    ->image()
    ->required() // this is working
    ->hiddenLabel()
    // below rules() is not working
    ->rules([
        // 'required',
        'dimensions:width=100,height=200',
        // Rule::dimensions()->width(10)->height(50),
    ]),
Screencast.from.03-18-2024.02.01.55.PM.webm

@danharrin
Copy link
Member

When #11907 is released, you'll be able to use nestedRecursiveRules() to pass these rules. The reason why it doesnt work is because the file upload always stores files in an array, even if there is only a singular file. So we need to pass these rules to each item in that array.

@valpuia604
Copy link
Contributor Author

valpuia604 commented Mar 19, 2024

Hi Dan, I've updated to 3.2.53 which contain #11907 and I use like below. But these validation are still not working.. Any idea

Forms\Components\FileUpload::make('img')
                    ->label('img')
                    ->image()
                    ->nestedRecursiveRules([
                        'required',
                        'dimensions:width=100,height=200',
                    ]),

@danharrin danharrin reopened this Mar 19, 2024
@github-project-automation github-project-automation bot moved this from Done to Todo in Roadmap Mar 19, 2024
@danharrin danharrin added enhancement New feature or request and removed bug Something isn't working unconfirmed labels Mar 19, 2024
@danharrin danharrin added this to the v3 milestone Mar 19, 2024
@danharrin
Copy link
Member

I've converted this to a feature request for a dimensions() method on the FileUpload which will allow us to investigate a better implementation

@danharrin danharrin changed the title FileUpload rules() with dynamic form components FileUpload dimensions() validation Mar 19, 2024
@valpuia604
Copy link
Contributor Author

valpuia604 commented Mar 22, 2024

For ease of checking dimensions, here are my finding in below environments

  • Laravel v11.0.8
  • Livewire v3.4.9
  • Filament v3.2.58
// rules() is working if Fileupload is not dynamic components
Forms\Components\FileUpload::make('img')
    ->rules([
        'dimensions:width=100,height=200',
    ]),

Screenshot from 2024-03-22 10-42-14

Using nestedRecursiveRules()

Forms\Components\FileUpload::make('img')
    ->nestedRecursiveRules([
        'dimensions:width=100,height=200',
    ]),

Screenshot from 2024-03-22 10-43-27

And now nestedRecursiveRules() seems to work (in dynamic component) which I said it's not working in #11904 (comment), but have problem in error message
Screenshot from 2024-03-22 10-53-15

@valpuia604
Copy link
Contributor Author

valpuia604 commented Mar 22, 2024

It's also seems like I cannot change validation messages which is inside rules() or nestedRecursiveRules() for FileUpload

Forms\Components\FileUpload::make('img')
    ->required()
    ->rules([
        'dimensions:width=100,height=200',
    ])
    ->validationMessages([
        'required' => 'Error!!!!', // working
        'dimensions' => 'Dimension error!', // not working
    ])

Cause this one is working fine

Forms\Components\TextInput::make('name')
    ->rules([
        'required', 'min:3',
    ])
    ->validationMessages([
        'required' => 'Error!!!!',
        'min' => 'Min error!!',
    ]),

@malzariey
Copy link
Contributor

It's also seems like I cannot change validation messages which is inside rules() or nestedRecursiveRules() for FileUpload

Forms\Components\FileUpload::make('img')
    ->required()
    ->rules([
        'dimensions:width=100,height=200',
    ])
    ->validationMessages([
        'required' => 'Error!!!!', // working
        'dimensions' => 'Dimension error!', // not working
    ])

Cause this one is working fine

Forms\Components\TextInput::make('name')
    ->rules([
        'required', 'min:3',
    ])
    ->validationMessages([
        'required' => 'Error!!!!',
        'min' => 'Min error!!',
    ]),

refer to this
https://livewire.laravel.com/docs/validation#custom-key

@binaryfire
Copy link
Contributor

binaryfire commented May 18, 2024

Just sharing this related discussion: #5037

Wouldn't it be better performance-wise to implement this on the frontend using FilePond's image validate size plugin?

https://pqina.nl/filepond/docs/api/plugins/image-validate-size/

My main use case would be setting a min and max rather than exact dimensions. That'd prevent over- or under- sized images from ever hitting the server. Which would really reduce bandwidth and load.

@polar-sh polar-sh bot added the Fund label Jun 3, 2024
@danharrin danharrin removed the fund label Jun 4, 2024
@simonhamp
Copy link

simonhamp commented Jun 20, 2024

Sorry to dive in on this one, it's the only related issue I could find before opening a new one... not sure if this actually related though.

I'm getting an issue when using a rule of 'dimensions:min_width=400,min_height=400'. When the validation fails, I get a bad gateway error locally (using Herd) and in prod it seems to trigger this TypeError: Illuminate\Translation\PotentiallyTranslatedString::__toString(): Return value must be of type string, null returned

I've had to remove the rule for now.

@juliancc
Copy link

Just sharing this related discussion: #5037

Wouldn't it be better performance-wise to implement this on the frontend using FilePond's image validate size plugin?

https://pqina.nl/filepond/docs/api/plugins/image-validate-size/

My main use case would be setting a min and max rather than exact dimensions. That'd prevent over- or under- sized images from ever hitting the server. Which would really reduce bandwidth and load.

Yeah this would be ideal. is this possible right now?

@danharrin danharrin modified the milestones: v3, v4.1 Jul 30, 2024
@danharrin danharrin modified the milestones: v4.1, v4, v4.0.0-alpha, v4.0.0 Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request low priority
Projects
Status: Todo
Development

Successfully merging a pull request may close this issue.

6 participants