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

Required validator does not work with file uploads #374

Open
juergenweb opened this issue Apr 17, 2023 · 4 comments
Open

Required validator does not work with file uploads #374

juergenweb opened this issue Apr 17, 2023 · 4 comments

Comments

@juergenweb
Copy link

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
Copy link
Author

juergenweb commented Aug 9, 2023 via email

@scoumbourdis
Copy link

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.

@juergenweb
Copy link
Author

juergenweb commented Aug 9, 2023 via email

@scoumbourdis
Copy link

Glad that I could help :)

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

No branches or pull requests

2 participants