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

Validation upload file #4524

Merged
merged 3 commits into from Sep 7, 2014
Merged

Validation upload file #4524

merged 3 commits into from Sep 7, 2014

Conversation

markstory
Copy link
Member

Add Validation::uploadedFile() as a simple wrapper around the various file upload validation methods. It also makes it easier to deal with optional files which was something that came up in #4456.

Add a simple wraper function for the various file validation methods.
This method makes it easier to wrap up the various file validators into
one method.

Refs #4456
This makes validating forms where there is an optional file much easier.
@markstory
Copy link
Member Author

These changes could easily be backported to 2.6 if people are interested.

if (!static::uploadError($file, $options['optional'])) {
return false;
}
if ($options['optional'] && (int)$file['error'] === UPLOAD_ERR_NO_FILE) {
Copy link
Member

Choose a reason for hiding this comment

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

I think for any other type of upload error the validator should return false.

Copy link
Member

Choose a reason for hiding this comment

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

Doh, I missed that uploadError() check is already done above.

@lorenzo
Copy link
Member

lorenzo commented Sep 7, 2014

Nice! looks good to me 👍

@Phally
Copy link
Contributor

Phally commented Sep 7, 2014

Awesome! This is going to bring everybody a lot of joy.

Also I check for extension of the uploaded file. That's quite common isn't it? So it might as well be in this wrapper. For reference, extensions were mentioned in #4456. Any particular reason you left it out?

@ADmad
Copy link
Member

ADmad commented Sep 7, 2014

@Phally Extension checking isn't really useful as they can be easily faked.

@ADmad
Copy link
Member

ADmad commented Sep 7, 2014

While having a single method to handle all file upload related validations is nice I would personally prefer separate methods for each. That why I can set seperate error messages for each and user can be notified of the exact problem.

@Phally
Copy link
Contributor

Phally commented Sep 7, 2014

@ADmad Yes it can be faked, but that isn't its purpose. The purpose is to tell the user they might have selected the wrong file or exported something in the wrong format. Maybe that's a good argument not to put it in this method so you can separate the error message as you said. Though for completeness I still feel it should be in this wrapper.

@ADmad
Copy link
Member

ADmad commented Sep 7, 2014

@Phally Fair enough, extension checking is more user centric.

@markstory
Copy link
Member Author

I think extension checking is secondary to mimetype checking, which is included. One drawback to this validation method is you cannot give fine grained error messages. However, if you need a simple pass/fail it couldnbe quite useful.

@markstory
Copy link
Member Author

@ADmad All the part needed to build fine grained validation are still there. It is a bit tricky to build optional file uploads which was one of the goals for ths method.

@ADmad
Copy link
Member

ADmad commented Sep 7, 2014

@markstory Indeed they are, so I am cool with merging this.

lorenzo added a commit that referenced this pull request Sep 7, 2014
@lorenzo lorenzo merged commit d07e363 into 3.0 Sep 7, 2014
@lorenzo lorenzo deleted the validation-upload-file branch September 7, 2014 13:46
@vanquang9387
Copy link

@markstory Can you please backport this validation to 2.x.
I have to stick with CakePHP 2.x for months.

@markstory
Copy link
Member Author

@vanquang9387 You're welcome to open a pull request with the backport. I'm pretty strapped for time right now.

@vanquang9387
Copy link

It's a bit difficult to me. Will give it a try 😊

vanquang9387 added a commit to vanquang9387/cakephp that referenced this pull request Oct 19, 2015
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.

None yet

5 participants