如何将$ _FILES绑定到Phalcon表单?

时间:2016-05-19 21:52:15

标签: php forms validation file-upload phalcon

我有这部分代码:

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
    }
}

有没有办法将$file传递给Phalcon \ Forms \ Form对象? 或者另一种做法是检查包含文件和文本的表单的有效性?

1 个答案:

答案 0 :(得分:1)

要验证文件,您必须使用FileValidator类作为此示例:

$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);