在 Yii 中验证单个文件(单个输入文件字段)没有问题,而且效果很好:
public function rules() {
return CMap::mergeArray(parent::rules(), array(
array(
'image',
'file',
'allowEmpty'=>false,
'types'=>'pdf,jpg,gif,png',
'on' => 'CaseA, CaseB',
'maxSize'=>1024000,
'tooLarge' => 'Max File size is 1MB',
'message' => 'You have to upload a file at least'
),
....
....
如果图像是一个文件数组怎么办?Yii 创建的表单元素看起来像这样(Firebug 输出):
<input type="file" id="MyFormModel[image][0]" name="MyFormModel[image][0]">
<input type="file" id="MyFormModel[image][1]" name="MyFormModel[image][1]">
我究竟应该在我的规则中修改什么?一个简单的“图像[]”没有帮助。
编辑:解决方案
public function rules() {
return CMap::mergeArray(parent::rules(), array(
array(
'image',
'file',
'allowEmpty'=>false,
'types'=>'pdf,jpg,gif,png',
'on' => 'CaseA, CaseB',
'maxSize'=>1024000,
'maxFiles' => 5, // <----- THAT'S IT
'tooLarge' => 'Max File size is 1MB',
'message' => 'You have to upload a file at least'
),
....
....