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

Regression in FileValidator since Yii 2.0.50 when validating required files #20231

Closed
marcovtwout opened this issue Jul 22, 2024 · 4 comments
Closed
Labels
Milestone

Comments

@marcovtwout
Copy link
Member

marcovtwout commented Jul 22, 2024

What steps will reproduce the problem?

['example', 'file', 'skipOnEmpty' => false],
  • Set enableClientValidation = false in the ActiveForm. (client side validation still works as expected and will prevent reproduction of the issue)
  • Post the form without selecting any file.

What is the expected result?

A validation error 'Please upload a file'.

What do you get instead?

The validation passes.

Additional info

The regression is introduced by the changes to validateAttribute() in #20167 which were released with Yii 2.0.50. It still works as expected on 2.0.49.

@bizley Since skipOnEmpty is set to false, validateOnAttribute() is called and should still handle the case where no file has been uploaded. The validation should be executed even if $minFiles is still set to the default of 0. Previously, this happened here, but this code is not reached anymore in the given scenario:

return [$this->uploadRequired, []];

Q A
Yii version 2.0.50
PHP version -
Operating system -
@bizley
Copy link
Member

bizley commented Jul 22, 2024

I'm a bit confused because I totally missed that logic. Is it stated only in this vardoc? To be honest I think this is very unclear for the users (= "I require a file and the required number of files must be at least 0"). I would only add upgrade note for this one. @samdark what do you think?

@marcovtwout
Copy link
Member Author

@bizley It's part of the main example in the definitive guide as well: https://www.yiiframework.com/doc/guide/2.0/en/input-file-upload#creating-models

Here's the client side validation for comparison https://github.com/yiisoft/yii2/blob/master/framework/assets/yii.validation.js#L404

Requiring to set minFiles might also confuse some users as its related to functionality you only need when you want the input to deal with multiple files (giving a specific $tooFew error message instead of the $uploadRequired message).

If you go looking in the API for required field functionality without knowing the design, the $uploadRequired parameter documents clearly how to set it. While I agree its not the best design and a bit implicit from the code alone, I'd favor backward compatibility in this case.

@bizley
Copy link
Member

bizley commented Jul 22, 2024

I see your point 👍🏻 Do you think changing line 212 to

if ($filesCount === 0 && $this->uploadRequired !== null) {

would be enough?

@marcovtwout
Copy link
Member Author

@bizley I think if ($filesCount === 0) would be enough. The field is defined as string only and similar code that uses validation texts does not check for null either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants