Validation on input file in cakephp

In cakephp, I try to check if the file input field has an attached file, and if it does not display an error. I did this with other fields, but just can't get this to work in this field.

Here is a model

 Array ('notempty'), 'uploadeduploaded_file' => array ('notempty')); ? >

and here is my ctp file

<?php echo $form->input('Uploaded.uploaded_file', array('type' => 'file', 'label' => 'Upload file', "label" => false)); ?>

I suppose this should be due to what I should call a field in the model, but I tried all sorts of combinations and can't make it work.

Any help would be appreciated.

+3
source share
1 answer

optional cakePHP check for file uploads

it may help u

or

var $validate = array(
            'imageupload' => array(
                'checksizeedit' => array(
                    'rule' => array('checkSize',false),
                    'message' => 'Invalid File size',

                ),
                'checktypeedit' =>array(
                    'rule' => array('checkType',false),
                    'message' => 'Invalid File type',

                ),
                'checkuploadedit' =>array(
                    'rule' => array('checkUpload', false),
                    'message' => 'Invalid file',

                ),
);



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){
        $data = array_shift($data);
        if(!$required && $data['error'] == 4){
            return true;
        }
        $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;
    }
+6
source

Source: https://habr.com/ru/post/1753285/