2

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 Answer 1

2

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the reply, I will check it at Monday, my project is at work :). I think I already tried with the required parameter and it didn't work. And if I add 'novalidate' to the options, wouldn't that cause it to skip my filesize and extension validations?
Okay, I got it working, the solution was 'required' => false in the input field options (not validation rules in the model). I will edit my post now.
I tried 'novalidate' and it was still marked as a required field. The problem was not in the Model validation settings, but in the view, that is why 'required' => false in the input field worked I guess. Btw, I didn't know what 'novalidate' does, thanks for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.