Skip to content

[Validator] Added allowEmpty option to File constraint #10615

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

Closed

Conversation

megazoll
Copy link
Contributor

@megazoll megazoll commented Apr 2, 2014

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR symfony/symfony-docs#3746

public $notFoundMessage = 'The file could not be found.';
public $notReadableMessage = 'The file is not readable.';
public $maxSizeMessage = 'The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}.';
public $mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.';
public $allowEmptyMessage = 'The file could not be empty.';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably better $disallowEmptyMessage

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The string could be Empty file is not allowed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually messages vars are equals with their options but with postfix.
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator/Constraints/Image.php#L40

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @romainneutron this name is confusing. One could also rename the option to disallowEmpty to avoid the problem.

@stof stof added the Validator label Apr 9, 2014
@@ -59,6 +59,7 @@ CHANGELOG
* added `Util\PropertyPath`
* made the PropertyAccess component an optional dependency
* deprecated `ValidatorBuilder::setPropertyAccessor()`
* added `allowEmpty` to File validator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this to a new 2.6 section?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@Tobion
Copy link
Contributor

Tobion commented Jun 6, 2014

What is the use-case for this? Uploading a file with only 1 byte is probably also invalid in your case. So after all the file must be validated anyway. And a more general minSize option would maybe be more suitable.

@megazoll
Copy link
Contributor Author

megazoll commented Jun 6, 2014

@Tobion #10507

@Tobion
Copy link
Contributor

Tobion commented Jun 6, 2014

I agree with Fabien that minSize is also awkward.
Actually I see no reason why any dev would want somebody to be able to upload an empty file. If there is nothing to upload, then the upload should be optional.
For this reason, I would just remove the "allowEmpty" option and simply always add an error when the file is empty. It's small bc break but allowing empty files makes no sense to me and having an option for something that makes no sense, doesn't make it better.

Also the message could then include an explanation of the possible problem like: "Empty file is not allowed. Maybe it's locked by another program."

@fabpot
Copy link
Member

fabpot commented Jun 15, 2014

Closing for reasons exposed by @Tobion

@fabpot fabpot closed this Jun 15, 2014
@Tobion
Copy link
Contributor

Tobion commented Jun 15, 2014

@megazoll would you make a PR for the different fix?

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

Successfully merging this pull request may close these issues.

5 participants