我有用于上传图像的输入类型文件。这不是必填字段。所以我将allowEmpty选项设置为 true。但是我的模型没有采用这个选项。验证码 :
public $validate = array(
'image' => array(
'isImage' => array(
'rule' => 'isImage',
'message' => 'Please upload images only',
'allowEmpty' => true,
//'required' => true,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
public function isImage($field = null) {
$file_info = $field['image_path'];
$image_types = array("image/jpeg", "image/gif", "image/jpg", "image/png");
$result = false;
if (in_array($file_info['type'], $image_types)) {
$result = true;
}
return $result;
}
或用于验证图像类型的任何其他规则。