4

I have this part of code:

if ($this->request->isPost() && $this->request->hasFiles()) {
    $post = $this->request->getPost();
    $file = $this->request->getUploadedFiles();
    if ($form->isValid($post)) {
        $form->bind((array) $this->request->getPost(), $entity);
        $entity->save();

        // Do some other stuff
    }
}

Is there a way to pass $file to a Phalcon\Forms\Form object? Or another practice to check if validity of a form that contains both files and text?

lazzy_ms
  • 1,169
  • 2
  • 18
  • 40
Mihai Petcu
  • 368
  • 2
  • 14
  • AFAIK, there is no built in method in Phalcon to validate a file upload via a form. You need to write some custom code for this. Try checking out this link https://forum.phalconphp.com/discussion/3583/how-to-validate-file-upload – Timothy May 23 '16 at 07:07

2 Answers2

1

To validate Files you must use the FileValidator Class as this example:

$file = new FileValidator([
        'maxSize' => '10M',
        'messageSize' => _('Your file is huge. Max allowed is 10 Mb'),
        'allowedTypes' => ["image/jpeg", "image/png"],
        'messageType' => _('File type is not permitted. Try with JPG, GIF, etc..'),
        'messageEmpty' => _('Cannot be empty. Please upload a file')
]);

$this->validator->add('filesInputName', $file);
$messages = $this->validator->validate($_FILES);
Infobuscador
  • 240
  • 1
  • 12
0

This is a really old question, but as it affects my current project I share my solution. Normally, I'm using form classes and validation classes, too. For validating a file upload along with other POST data, I simply combine both payloads:

$entity = new MyCustomModel();
$merged = array_merge($_POST, $_FILES);
if ($form->isValid($merged, $entity)) {
    // ...
}
rabudde
  • 7,498
  • 6
  • 53
  • 91