Cakephp: Multiple files upload field sets to requi

2019-07-17 07:21发布

问题:

Simple problem, but can't find my solution. I have a gallery upload field with multiple files like this:

echo $this->Form->input('Item.gallery.', array(
    'label' => __('Gallery'),
    'type' => 'file',
    'multiple' => 'multiple',
));

And i got the validation rules like this:

'gallery' => array(
    'fileSize' => array(
        'rule' => array('fileSize', '<=', '1MB'),
        'message' => 'Image must be less than 1MB',
        'allowEmpty' => true,
        'last' => false,
    ),
    'extension' => array(
        'rule' => array('extension', array('gif', 'jpeg', 'png', 'jpg')),
        'message' => 'Invalid image',
    ),
),

If i put a single file upload field, then it is not required. When i set it to multiple, it is required and I don't want it to be. How do i do this?

EDIT:

The catch was setting 'required' => false in the input field options, in the .ctp file:

echo $this->Form->input('Item.gallery.', array(
    'label' => __('Gallery'),
    'type' => 'file',
    'multiple' => 'multiple',
    'required' => false,
));

The validation rules in the model remained the same.

回答1:

Try adding 'novalidate' to your options array in $this->Form->create();. This disables HTML5 validation.

I expect that will work but if not try adding 'required' => false to your validation rules (goes by field not rule so you only need it in one of the rules not both)