2014-10-09 54 views
1

有沒有什麼辦法讓不要求在CakePHP中 我已經試過allowEmpty設置爲true,並FLASE必需的,但沒有奏效可選的文件上傳驗證

'pic' => array(
       'uploadError'=>array(  
           'rule'=>'uploadError', 
           'message'=>'The image upload failed', 
           'allowEmpty'=>true 
          ) 
      ) 
+0

的可能重複[CakePHP的表單驗證只在輸入數據(http://stackoverflow.com/questions/24094712/cakephp-form-validation-only-on-entering-data) – ndm 2014-10-09 20:19:02

回答

1

嘗試設置你的HTML屬性文件上傳在視圖中的HTML元素上必須爲false。

下面是一些例子:

蛋糕:

echo $this->Form->input('pic', array('type' => 'file', **'required' => false**)); 

對於原生PHP:

<input type="file" name="data['Upload']['pic']" **required**> 
+0

沒有什麼改變不幸 – Exchanger13 2014-10-10 01:14:23

+0

我認爲我應該使用「驗證之前」的東西,但在cakephp文檔中沒有足夠的信息 – Exchanger13 2014-10-10 01:18:01

+0

你能試試嗎?我想這和它的工作:) 'PIC'=>數組( \t \t \t '必要'=>數組( \t \t \t 'allowEmpty') \t \t) \t); – webartisan 2014-10-10 01:51:26

1

嘗試在view.ctp使用此代碼。我認爲需要的默認值在CakePHP的最新版本中是false或者你可以把驗證在你的Model中?

<?php echo $this->Form->input('pic', array('type' => 'file', 'required' => false)); ?> 
+0

@ndm給出的代碼http://stackoverflow.com/questions/24094712/cakephp-form-validation-only-on-entering-data 有用的信息,閱讀第二個答案 – Exchanger13 2014-10-10 01:26:02

+0

但我應該在哪裏使用beforeValidate ? – Exchanger13 2014-10-10 01:28:33

+0

嘗試閱讀答案,也許它會幫助你。 http://stackoverflow.com/questions/23777089/how-to-uploading-images-in-cakephp-2-0 – r3mmel 2014-10-10 01:36:41