【问题标题】:File Upload validation only on record create, not edit文件上传验证仅在记录创建时,而不是编辑
【发布时间】:2010-10-01 19:55:07
【问题描述】:

我有一个允许文件上传的表单,我正在使用数据验证来检查它是什么类型的文件,这一切都很好。然后我想做的是允许他们编辑相同的记录,但不需要当时上传文件。

我的验证规则如下:

    'header_pic' => array(
        'extension' => array(
            'rule' => array('extension', array('jpeg', 'jpg', 'gif', 'png')),
            'message' => 'You must supply a GIF, PNG, or JPG file.',
        )
    )

但是这个规则需要一个文件。我找到了“on”参数,可以将其添加到此规则中,但它只会在创建时检查文件。不在编辑中。

我试过了,但没用:

   'header_pic' => array(
        'extension' => array(
            'rule' => array('extension', array('jpeg', 'jpg', 'gif', 'png')),
            'required' => false,
            'allowEmpty' => true,
            'message' => 'You must supply a GIF, PNG, or JPG file.',
        ),
        'notEmpty' => array(
            'rule' => array( 'notEmpty'),
            'on' => 'create',
            'message' => 'You must supply a file.',
        ),
    )

我错过了什么? (提前致谢!!)

【问题讨论】:

    标签: php cakephp cakephp-1.3


    【解决方案1】:
    var $validate = array(
            'imageupload' => array(
                'checksizeedit' => array(
                    'rule' => array('checkSize',false),
                    'message' => 'Invalid File size',
                    'on' => 'update'
                ),
                'checktypeedit' =>array(
                    'rule' => array('checkType',false),
                    'message' => 'Invalid File type',
                    'on' => 'update'
                ),
                'checkuploadedit' =>array(
                    'rule' => array('checkUpload', false),
                    'message' => 'Invalid file',
                    'on' => 'update'
                ),
                'checksize' => array(
                    'rule' => array('checkSize',true),
                    'message' => 'Invalid File size',
                    'on' => 'create'
                ),
                'checktype' =>array(
                    'rule' => array('checkType',true),
                    'message' => 'Invalid File type',
                    'on' => 'create'
                ),
                'checkupload' =>array(
                    'rule' => array('checkUpload', true),
                    'message' => 'Invalid file',
                    'on' => 'create'
                ),
            )
        );
    
    
    
    
    
    
    function checkUpload($data, $required = false){
            $data = array_shift($data);
            if(!$required && $data['error'] == 4){
                return true;
            }
            //debug($data);
            if($required && $data['error'] !== 0){
                return false;
            }
            if($data['size'] == 0){
                return false;
            }
            return true;
    
            //if($required and $data)
        }
    
        function checkType($data, $required = false,$allowedMime = null){
            $data = array_shift($data);
            if(!$required && $data['error'] == 4){
                return true;
            }
            if(empty($allowedMime)){
                $allowedMime = array('image/gif','image/jpeg','image/pjpeg','image/png');
            }
    
            if(!in_array($data['type'], $allowedMime)){
                return false;
            }
            return true;
        }
    
        function checkSize($data, $required = false){
            $data = array_shift($data);
            if(!$required && $data['error'] == 4){
                return true;
            }
            if($data['size'] == 0||$data['size']/1024 > 2050){
                return false;
            }
            return true;
        }
    

    我也用过这个 我成功了 试试。 如果需要,请进行更改。 万事如意

    【讨论】:

      【解决方案2】:

      我想通了。 (感谢 omabena 和 RSK 的建议。)我发现我可以设置两个验证规则,一个用于添加,一个用于创建。工作得非常干净。

         'header_pic' => array(
              'extension' => array(
                  'rule' => array('extension', array('jpeg', 'jpg', 'gif', 'png')),
                  'message' => 'You must supply a GIF, PNG, or JPG file.',
                  'required' => false,
                  'on' => 'add'
              ),
              'extension2' => array(
                  'rule' => array('extension', array('jpeg', 'jpg', 'gif', 'png')),
                  'message' => 'You must supply a GIF, PNG, or JPG file.',
                  'required' => true,
                  'on' => 'create'
              ),
          )
      

      【讨论】:

        【解决方案3】:

        我有同样的问题,我的解决方案不是很好,但它对我有用,我所做的是,允许空 => 为真。在控制器的编辑功能中,我将该图像的数据传递给 $this->data 数组,类似这样,所以当你转到表单时,你已经有了包含一些数据的“图像”字段,并且您不必再次上传图像或修改它。

        如果不清楚,请告诉我,我会尝试分解它。

        希望对你有用

        function admin_edit($id = null) {
                if (!$id && empty($this->data)) {
                        $this->Session->setFlash(__('Invalid category', true));
                        $this->redirect(array('action' => 'index'));
                }
                if (!empty($this->data)) {
        
                    if(!empty($this->data['Category']['image']['name']))
                    {
                        $image_name = $this->Picture->upload_image_and_thumbnail($this->data,"image",570,70,"categories",true,"Category");
                        $this->data['Category']['image'] =  $image_name;
                    }
                    else{
                        $image_name = $this->Category->find('first',array(
                           'conditions'=> array(
                               'Category.id' => $this->data['Category']['id']
                           ),
                            'fields' => array(
                                'Category.image'
                            )
                        ));
                        $this->data['Category']['image'] = $image_name['Category']['image'];
                    }
        
        
                        if ($this->Category->save($this->data)) {
                                $this->Session->setFlash(__('The category has been saved', true));
                                $this->redirect(array('action' => 'index'));
                        } else {
        
                                $this->Session->setFlash(__('The category could not be saved. Please, try again.', true));
                        }
                }
                if (empty($this->data)) {
                        $this->data = $this->Category->read(null, $id);
                }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-05-26
          • 1970-01-01
          • 1970-01-01
          • 2015-10-27
          • 1970-01-01
          • 2010-11-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多