Hello, i’m trying to use the new Form class, but i am stuck on the attachment validation, i don’t know how to replicate the one that is explained in the cookbook for previous method.
Printing $form->toFormValues() show me that file is an array, with the uploaded file name inside of it, but i don’t know how to access it to validate its size.
The cookbook article you link isn’t using the Form class, so not sure how you ended there. I don’t think the files field in the form class will let you validate file size as the field itself only stores the name of the files and things like required, min, max.
The cookbook article you link isn’t using the Form class, so not sure how you ended there
I understand that, but the resource linked is the only one that explains how to handle file attachments with forms and their validation. What I’d like is to achieve the same file validation shown in the cookbook, but using the new Form class.
I don’t think the files field in the form class will let you validate file size as the field itself only stores the name of the files and things like required, min, max.
So, is there no way to validate file uploads with the new Form class in the same way it’s shown in the cookbook example?
// get the uploads
$uploads = $kirby->request()->files()->get('file');
// loop through uploads and check if they are valid
foreach ($uploads as $upload) {
// make sure the user uploads at least one file
if ($upload['error'] === 4) {
$alerts[] = 'You have to attach at least one file';
// make sure there are no other errors
} elseif ($upload['error'] !== 0) {
$alerts[] = 'The file could not be uploaded';
// make sure the file is not larger than 2MB…
} elseif ($upload['size'] > 2000000) {
$alerts[] = $upload['name'] . ' is larger than 2 MB';
// …and the file is a PDF
} elseif ($upload['type'] !== 'application/pdf') {
$alerts[] = $upload['name'] . ' is not a PDF';
// all valid, try to rename the temporary file
} else {
$name = $upload['tmp_name'];
$tmpName = pathinfo($name);
// sanitize the original filename
$filename = $tmpName['dirname']. '/'. F::safeName($upload['name']);
if (rename($upload['tmp_name'], $filename)) {
$name = $filename;
}
// add the files to the attachments array
$attachments[] = $name;
}
}
@texnixe I think that impression comes from the v5 release page where we announced that we refactored the Form class a lot.
@giorgiodare The files field deals with already uploaded files and in that case also really file objects in the Kirby sense (not any front-end uploaded file). In the Panel, we use the Kirby\Api\Upload class to handle the file upload and related validations: kirby/src/Api/Upload.php at main · getkirby/kirby · GitHub
But also here keep in mind that this is designed to handle uploads that get turned into Kirby file objects with file blueprints etc. - this probably currently then still has limitations to re-use it for your frontend form.
While using JavaScript in the frontend has advantages like immediate reaction without backend calls, it should always only be a first step, and final validation be done in the backend.