cakephp文件字​​段验证

时间:2013-05-16 09:11:35

标签: cakephp cakephp-2.0

我试图在模型中的cakephp中验证文件字段,并在创建和更新时使用有效扩展名尝试仅在字段不为空时验证文件。创建验证工作正常,但在更新时验证if字段为空。我想仅在字段不为空时才验证扩展 这是模型验证数组中的验证规则

'image' => array(
        'rule1'=>array(
            'rule' => array('extension',array('jpeg','jpg','png','gif')),
            'required' => 'create',
            'allowEmpty' => true,
            'message' => 'Select Valid Image',
            'on' => 'create',
            'last'=>true
        ),
        'rule2'=>array(
            'rule' => array('extension',array('jpeg','jpg','png','gif')),
            //'required' => 'create',
            'allowEmpty' => true,
            'message' => 'Select Valid Image',
            'on' => 'update',
        ),
    ),

2 个答案:

答案 0 :(得分:4)

以下是在创建时使用required验证图像字段的正确方法,并且可以在更新图像字段时允许空白

图像字段验证数组

'image' => array(
    'rule1'=>array(
        'rule' => array('extension',array('jpeg','jpg','png','gif')),
        'required' => 'create',
        'allowEmpty' => true,
        'message' => 'Select Valid Image',
        'on' => 'create',
        'last'=>true
    ),
    'rule2'=>array(
        'rule' => array('extension',array('jpeg','jpg','png','gif')),
        'message' => 'Select Valid Image',
        'on' => 'update',
    ),
),

在更新操作中取消设置之前取消设置图像字段

function beforeValidate($options = array()){
    if(empty($this->data[$this->alias]['id']))
    {
        return true;
    }
    else
    {
        if(empty($this->data[$this->alias]["image"]["name"])){
        unset($this->data[$this->alias]["image"]);
        }
        return true; //this is required, otherwise validation will always fail
    }
}

答案 1 :(得分:1)

我们可以通过自定义验证来实现,如下所示

public $validate =array(
    'image' => array(
        'rule' => array('checkValidImage'),
        )
    );


 public function checkValidImage($field)
      {

        $extension = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png', 'image/png', 'image/jpg');
        $isValidFile = in_array($field['new_image']['type'], $extension);
        $errors = array();
        $editMethod = false;
        if(!empty($this->data['ModelName']['id'])) // It will work for Update Method
        {
          if(!empty($this->data['ModelName']['image']['name'] ))
          {
            if (($field['image']['error'] == 1)) 
            {
                $errors [] = "Please upload jpg,png or gif files with size 2 MB.";
            }
            else if (empty($field['image']['name']))
            {
                $errors [] = "Please upload image";
            } 
            else if ($field['image']['size'] >= 2097152) {
                $errors [] = "Please upload jpg,png or gif files with size 2 MB.";
            }
            else if ($isValidFile !=1)
            {

                $errors [] = "Please select file in gif,jpeg,png format.";
            }

          }else
                {
                    $errors [] = "Please select file in gif,jpeg,png format.";
                }

        }
        else
        {
            if(!empty($this->data['ModelName']['image']['name'] )) // It will work for Create Method
            {
                if (($field['image']['error'] == 1)) 
                {
                    $errors [] = "Please upload jpg,png or gif files with size 2 MB.";
                }
                else if (empty($field['ModelName']['name']))
                {
                    $errors [] = "Please upload image";
                } 
                else if ($field['ModelName']['size'] >= 2097152) {
                    $errors [] = "Please upload jpg,png or gif files with size 2 MB.";
                }
                else if (!(in_array($field['image']['type'], $extension)))
                {
                    $errors [] = "Please select file in gif,jpeg,png format.";
                }

            }



        }

        if (!empty($errors))
        {
            return implode("\n", $errors);
        }
        return true;
    }