File Upload validation only on record create, not edit

懵懂的女人 提交于 2019-12-03 23:16:00
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;
    }

I used this for the same and I am successfully have a try. And do the changes if u need. all de best

I figured it out. (Thanks to omabena and RSK for their suggestions.) I figured out I can just set up two verification rules and use one for the add and one for the create. Works pretty cleanly.

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

i had the same issue, my solution it's not really good but it worked for me, what i did is, to allowEmpty => true. and in the edit function on controller, i passed the data of that image, to the $this->data array, something like this, so when you go to the form, you already have the field 'image' with some data, and you don't have to upload the image again, or modify it.

If it's not clear, just tell me and i'll try to break it down.

I hope it works for you

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);
        }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!