【发布时间】:2013-10-03 19:11:36
【问题描述】:
我有一个用户可以上传图片的表单,我将它打印到页面上,如下所示:
<?php echo $this->Form->label('file', 'Image file', array('class' => 'col-lg-1 control-label')); ?>
然后,我在模型中设置验证,如下所示:
public $validate = array(
'file' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'You must select an image to upload'
),
'extension' => array(
'rule' => array('extension', array('png')),
'message' => 'Images must be in PNG format'
),
'size' => array(
'rule' => array('fileSize', '<', '1MB'),
'message' => 'Images must be no larger than 1MB'
),
'goodUpload' => array(
'rule' => 'uploadError',
'message' => 'Something went wrong with the upload, please try again'
)
)
);
但是,Cake 似乎没有将表单字段与验证规则相关联,就像我选择要上传的图像一样,我总是收到“您必须选择要上传的图像”作为表单错误。我已经确定表格有enctype="multipart/form-data"。
这是因为file 不是数据库字段吗?如何让 cake 在 file 上运行验证?
编辑:根据要求,这是我的完整表格:http://pastebin.com/SbSbtDP9
【问题讨论】:
-
您可以发布整个表单的代码吗?
-
当然,必须上传到 Pastebin,因为它很长。
-
您是否查看过此页面上的验证上传部分:book.cakephp.org/2.0/en/core-libraries/helpers/form.html
-
你能不能把你的控制器代码也放上来
标签: php forms validation cakephp