1

is there any way to make file uploading not required in cakephp i've tried setting allowEmpty to true , and required to flase but didn't work

'pic' => array(
              'uploadError'=>array(      
                              'rule'=>'uploadError',
                              'message'=>'The image upload failed',
                              'allowEmpty'=>true  
                             )
             )
Exchanger13
  • 337
  • 1
  • 2
  • 13
  • possible duplicate of [CakePHP Form Validation only on Entering Data](http://stackoverflow.com/questions/24094712/cakephp-form-validation-only-on-entering-data) – ndm Oct 09 '14 at 20:19

2 Answers2

1

Try setting your HTML attribute Required to false on the HTML element in your View.

Here are some examples:

For Cake:

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

For native PHP:

<input type="file" name="data['Upload']['pic']" **required**>
webartisan
  • 536
  • 3
  • 9
  • I think i should use something called "before Validate" but there is no enough info about it in the cakephp documentation – Exchanger13 Oct 10 '14 at 01:18
  • Can you try this? I tried this and it worked :) 'pic' => array( 'required' => array( 'allowEmpty') ) ); – webartisan Oct 10 '14 at 01:51
1

Try to use this code in your view.ctp. I think the default value of required in latest version of CakePHP is false or maybe you put validation in your Model?

<?php echo $this->Form->input('pic', array('type' => 'file', 'required' => false)); ?>
r3mmel
  • 656
  • 4
  • 13
  • the code that @ndm gave http://stackoverflow.com/questions/24094712/cakephp-form-validation-only-on-entering-data has useful info , read the second answer – Exchanger13 Oct 10 '14 at 01:26
  • but where should i use beforeValidate ? – Exchanger13 Oct 10 '14 at 01:28
  • try to read the answer, maybe it will help you. http://stackoverflow.com/questions/23777089/how-to-uploading-images-in-cakephp-2-0 – r3mmel Oct 10 '14 at 01:36