我正在尝试按以下方式验证Phalcon表单中的文件:
$facilityimage = new File('facilityimage');
$facilityimage->addValidators([
new FileValidator([
'maxSize' => '0.5M',
'messageSize' => 'Your image is too big. Max file size: 500KB',
'allowedTypes' => array('image/jpeg', 'image/png'),
'messageType' => 'Your image must be a JPEG or PNG file',
'equalResolution' => '130x80',
'messageEqualResolution' => 'Image resolution should be 130px by 80px.',
'allowEmpty' => (!empty($options['edit']) ? true : false)
])
]);
$this->add($facilityimage);
然后在我的控制器中
if (!$form->isValid(array_merge($this->request->getPost(), $_FILES))) {
foreach ($form->getMessages() as $message)
$this->flash->error((string) $message);
}
该代码在create动作中起作用,其中没有实体与表单关联。但是,当在编辑操作中将一个实体与之关联时,如果解析验证失败,则该元素会中断,因为上载的文件的新值尚不应该与该表单关联。
任何导致该解决方案的方法都会受到赞赏。