【问题标题】:cakephp2.X validation on edit view编辑视图上的 cakephp.2 验证
【发布时间】:2015-11-26 08:14:56
【问题描述】:

当我创建一个项目时,图像上传验证工作正常,但是当我编辑项目时,它总是要求我再次上传文件。使用 firebug 我检查了 img_upload 输入,它的值为“Lighthouse.jpg”。

我该如何克服这个问题?

在我的模型验证下方

     'img_upload' = array(
         'extension' => array(
           'rule' => array('extension', array('png','jpg','jpeg')),
           'message' => "Only png,jpg,jpeg Files Allowed",
         )
      );

在我的html代码下面:

echo $this->Form->create('Project',array('type'=>'file'));
echo $this->Form->input('img_upload'); 
echo $this->Form->end('Submit');

【问题讨论】:

  • 此验证规则适用于文件类型输入。

标签: php html cakephp file-upload


【解决方案1】:

您当前的规则是将文件上传设为必填字段,因此如果文件在编辑时未上传,则会出错。尝试将'allowEmpty' => true 添加到您的验证规则中以防止它被要求:-

 'img_upload' = array(
     'extension' => array(
         'rule' => array('extension', array('png','jpg','jpeg')),
         'message' => "Only png,jpg,jpeg Files Allowed",
         'allowEmpty' => true,
     ) 
 );

【讨论】:

    【解决方案2】:

    您好,尝试更改您的输入代码

    echo $this->Form->input('img_upload'); 
    

    到这里

    echo $this->Form->input('img_upload','type'=>'file'); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多