-
-
Notifications
You must be signed in to change notification settings - Fork 250
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
Required validator does not work with file uploads #374
Comments
Thanks for your tipp!
I hope that the author will add an additional check for files to the required validator. Writing and using a custom validator which, checks for the presence and value of the name attribute would be possible, but it would be much nicer to be able to use the default required validator 😊
Best regards
Von: John Skoumbourdis ***@***.***>
Gesendet: Dienstag, 8. August 2023 08:25
An: vlucas/valitron ***@***.***>
Cc: juergenweb ***@***.***>; Author ***@***.***>
Betreff: Re: [vlucas/valitron] Required validator does not work with file uploads (Issue #374)
Nice one. Just to add some info into this one in case the author is interested in the future. From a quick debug that I have done. Seems that you can also rely on the "name" attribute. You can check if the:
$_FILES['my_field_name']['name']
is empty. So I've checked that if you have an empty file upload to a non-multiple field. For example:
<input type="file" name="my_field_name"/>
The result is an empty string.
And when you have:
<input type="file" name="my_field_name" multiple />
Then you have an empty array. So a simple check for the name: !empty() will do the trick 😉
—
Reply to this email directly, view it on GitHub <#374 (comment)> , or unsubscribe <https://github.com/notifications/unsubscribe-auth/ACJKFRK76GKWXQPCFA52YHTXUHLUZANCNFSM6AAAAAAXA6V63E> .
You are receiving this because you authored the thread. <https://github.com/notifications/beacon/ACJKFRO54BGLOSBMENLPSB3XUHLUZA5CNFSM6AAAAAAXA6V63GWGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3TUL5UWJTTDPKQSG.gif> Message ID: ***@***.*** ***@***.***> >
|
Ha! I removed the comment because I thought that it will not be that useful. Thanks @juergenweb for the output. By the way for this I have done a work-around for my library (Grocery CRUD) which goes like this. I've added a new rule:
but just because the it will never reach this due to this condition: https://github.com/vlucas/valitron/blob/master/src/Valitron/Validator.php#L175 I am changing the data with a copy of the array ONLY FOR THE VALIDATION with something like this:
where
So at the callback that I call later I am also doing a check for this empty string. You can do something similar for the empty array. |
Nice! I will give this a try.
Unfortunately I guess Vlucas is not really maintaining the repository anymore, because the last change is 10 month ago and there are a lot of open issues.
So it will be up to us to solve this on our own, but your approach seems to be a good solution!
Best regards
Von: John Skoumbourdis ***@***.***>
Gesendet: Mittwoch, 9. August 2023 08:06
An: vlucas/valitron ***@***.***>
Cc: juergenweb ***@***.***>; Mention ***@***.***>
Betreff: Re: [vlucas/valitron] Required validator does not work with file uploads (Issue #374)
Ha! I removed the comment because I thought that it will not be that useful. Thanks @juergenweb <https://github.com/juergenweb> for the output. By the way for this I have done a work-around for my library (Grocery CRUD) which goes like this. I've added a new rule:
Validator::addRule('requiredUpload', $this->_requiredUploadCallback, 'must contain a file.');
but just because the it will never reach this due to this condition: https://github.com/vlucas/valitron/blob/master/src/Valitron/Validator.php#L175 I am changing the data with a copy of the array ONLY FOR THE VALIDATION with something like this:
if (empty($data[$fieldName])) {
$data[$fieldName] = self::UPLOAD_FIELD_EMPTY_STRING;
}
where UPLOAD_FIELD_EMPTY_STRING is this one:
const UPLOAD_FIELD_EMPTY_STRING = 'EMPTY_STRING';
So at the callback that I call later I am also doing a check for this empty string. You can do something similar for the empty array.
—
Reply to this email directly, view it on GitHub <#374 (comment)> , or unsubscribe <https://github.com/notifications/unsubscribe-auth/ACJKFROHODDG5RGKML5ULUDXUMSD3ANCNFSM6AAAAAAXA6V63E> .
You are receiving this because you were mentioned. <https://github.com/notifications/beacon/ACJKFRJH7NS5NEZPE3N2LRDXUMSD3A5CNFSM6AAAAAAXA6V63GWGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3TUL5UWJTTDSVBZ2.gif> Message ID: ***@***.*** ***@***.***> >
|
Glad that I could help :) |
The required rule does not check arrays. $_FILES is an array, so it would be great if it would be possible to add additional conditions to check first if a file array is present and if yes, wheter it is empty or not.
I know it will be more complex, because you cannot check if the array is empty, because $_FILES is never empty, so you have to check for 'size' or 'error' key.
The text was updated successfully, but these errors were encountered: