views:

50

answers:

3

I've got a form that allows a file upload and I'm using data validation to check what type of file it is and that's all good. What I want to do is then allow them to edit that same record but a file upload at that time is not needed.

My validation rule is as follows:

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

But this rule requires a file. I found the 'on' parameter and I could add it to this rule but it would then only check files on creation. Not on editing.

I tried this but it didn't work:

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

What am I missing? (Thanks in advance!!)

A: 

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

}
omabena
A: 
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

RSK
A: 

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'
        ),
    )
Dan Berlyoung