如何验证上传文件的数组?

时间:2013-11-08 08:52:54

标签: php validation file-upload yii

要验证单个文件(单个输入文件字段)在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创建的Form-Elements看起来像这样(Firebug-Output):

<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'
        ),
        ....
        ....

1 个答案:

答案 0 :(得分:0)

适当地设置maxFiles属性,验证器适用于所有输入。您无需更改输入控件名称。