bmcclure / CakePHP-Media-Plugin

A CakePHP (2.0) plugin enabling transfer/manipulation/embedding of files in 23 ways.
Other
60 stars 24 forks source link

File Upload shoud be required #7

Closed PHANTOMIAS closed 11 years ago

PHANTOMIAS commented 12 years ago

I have a file upload, but i want that it is a mandatory field, so the file upload is required.

In my model I tried this:

public $validate = array( 'file' => array( 'required' => true, 'extension' => array('rule' => array('checkExtension', false, array('jpg', 'jpeg', 'png', 'tif', 'tiff', 'gif'))), 'mimeType' => array('rule' => array('checkMimeType', false, array('image/jpeg', 'image/png', 'image/tiff', 'image/gif'))) ) );

But an error occurs in TransferBehavior.php:

public function setup($Model, $settings = array()) { /* If present validation rules get some sane default values */ if (isset($Model->validate['file'])) { $default = array('allowEmpty' => true, 'required' => false, 'last' => true);

        foreach ($Model->validate['file'] as &$rule) {
            $rule = array_merge($default, $rule);
        }
    }

...

This line fails $rule = array_merge($default, $rule); I think because it tries to merge my "required" with the "required" which is defined in this method. So, of course I can change the $default, but it is not very nice especially updating the plugin, I have to change it every time :-(

Are there any better solutions?

PHANTOMIAS commented 12 years ago

I changed it to required set to true, and allowEmpty to false. Adding a record is fine, I want to have that the user uploads an image. But editing this record, the user is forced to upload the image again, because it is required. But I do not want this, if the user won't update the image, it must be possible.

So, what can I do to solve this problem?

PHANTOMIAS commented 12 years ago

I solved it, but not very fancy, because I had to change code of the plugin:

if (isset($Model->validate['file'])) { if (Configure::read('MyTest.action') == 'add') { $default = array('allowEmpty' => false, 'required' => true, 'last' => true); foreach ($Model->validate['file'] as &$rule) { $rule = array_merge($default, $rule); } } } So, if the action is "add", I write in it my Controller into MyTest.action and required is set to true, otherwise (in my edit case), the default values were taken, so required is false. If anyone has a better solution, please tell me.

ndm2 commented 11 years ago

From what I can see the rules where simply not setup correctly, as ugly as it seems, but the when using multiple rules per field (which is what the transfer behavior currenly expects), then required still has to go in an actual ruleset. So instead of

public $validate = array(
    'file' => array(
        'required' => true,
        'extension' => array(
            'rule' => array('checkExtension', false, array('jpg', ...))
        ),
        ...
    )
);

it should be like this:

public $validate = array(
    'file' => array(
        'extension' => array(
            'required' => true,
            'rule' => array('checkExtension', false, array('jpg', ...))
        ),
        ...
    )
);

And in order to make it required on create only, set required to create instead of true (supported as of CakePHP 2.1). See http://book.cakephp.org/2.0/en/models/data-validation.html for more information.

bmcclure commented 11 years ago

Thanks for the details @ndm2! It sounds like this should be solved now, but don't hesitate to re-open if necessary.