【问题标题】:Allow no file when validation an uploaded file - CakePHP 2验证上传的文件时不允许文件 - CakePHP 2
【发布时间】:2018-05-29 19:23:47
【问题描述】:

我正在为一篇文章上传一张图片,并在模型中为此图片设置了一些验证规则。

//View
$this->Form->input('Post.image');

//Model Post
public $validate = array(
    'image' => array(
        'extension' => array(
            'rule' => array('extension', array('jpeg', 'jpg', 'png', 'gif', 'bmp')),
            'message' => "L'extension de l'image envoyée n'est pas valide"
        ),
        'uploadError' => array(
            'rule' => array('uploadError', true),
            'message' => "Une erreur est survenue lors de l'upload de l'image"
        ),
        'mimeType' => array(
            'rule' => array('mimeType', array('image/png', 'image/jpeg', 'image/jpeg', 'image/jpeg', 'image/gif', 'image/bmp')),
            'message' => "Type de l'image invalide"
        ),
        'fileSize' => array(
            'rule' => array('fileSize', '<=', '2MB'),
            'message' => "La taille de l'image ne doit pas dépasser 2MB"
        )
    )
);

图像不是required,但在html视图中生成的输入包含required属性,我通过在每个规则上添加allowEmpty =&gt; true来解决这个问题。但是,验证仍然没有通过!

我该如何解决这个问题?

谢谢

【问题讨论】:

标签: php file validation cakephp cakephp-2.0


【解决方案1】:

您不应该包含'required' =&gt; false 而不是使用'allowEmpty' =&gt; true 吗?根据我对CookBook regarding allowEmpty 的理解,它通过运行!empty($value) || is_numeric($value) 进行检查,而提交文件输入将导致数组。因此,考虑到当前的验证规则,$value 仍然是正确的并且仍然是必需的。

希望至少能以任何方式帮助您。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    相关资源
    最近更新 更多