valitron icon indicating copy to clipboard operation
valitron copied to clipboard

Required validator does not work with file uploads

Open juergenweb opened this issue 1 year ago • 4 comments

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.

juergenweb avatar Apr 17 '23 10:04 juergenweb

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:

The result is an empty string.

And when you have:

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 https://github.com/vlucas/valitron/issues/374#issuecomment-1668981027 , 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: @.*** @.***> >

juergenweb avatar Aug 09 '23 04:08 juergenweb

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:

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.

scoumbourdis avatar Aug 09 '23 06:08 scoumbourdis

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 https://github.com/vlucas/valitron/issues/374#issuecomment-1670726557 , 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: @.*** @.***> >

juergenweb avatar Aug 09 '23 06:08 juergenweb

Glad that I could help :)

scoumbourdis avatar Aug 09 '23 06:08 scoumbourdis