Cakephp:自动将多个文件上传字段集设置为必需 [英] Cakephp: Multiple files upload field sets to required automatically

查看:102
本文介绍了Cakephp:自动将多个文件上传字段集设置为必需的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题,但找不到我的解决方案。我有一个包含多个文件的库上传字段,例如:

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

我得到了这样的验证规则:

 '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'=& image',
),
),



如果我放一个文件上传字段,则不需要。当我把它设置为多个,它是必需的,我不想要它。



code>'required'=>

  echo $ this->在输入字段选项中输入false  Form-> input('Item.gallery。',array(
'label'=> __('Gallery'),
'type'=>'file',
'multiple'=>'multiple',
'required'=> false,
));

模型中的验证规则保持不变。

尝试向 $ this->中的选项数组添加'novalidate' > create(); 。这将禁用HTML5验证。



我期望这将工作,但如果不尝试添加'required'=> false 到你的验证规则(按字段不是规则,所以你只需要它在一个规则,而不是两个)


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.

解决方案

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)

这篇关于Cakephp:自动将多个文件上传字段集设置为必需的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆