jonny
(Jonny)
July 24, 2013, 12:09am
1
Hi
I have a file upload that is required as part of a form. I have the following rules
array('file', 'file', 'on' => 'insert',
'allowEmpty' => false,
'safe'=>true,
'maxSize'=> 512000,
'maxFiles'=> 1,
'mimeTypes' => 'application/msword, text/plain',
'tooLarge'=> 'file cannot be larger than 500KB.',
'wrongMimeType'=> 'Format must be: .doc .txt'
),
However when I leave the file upload blank it still tries to process the form.
<?php $form = $this->beginWidget('GxActiveForm', array( // Giix extension
'id' => 'file-form',
'enableAjaxValidation' => false,
'htmlOptions'=>
array('enctype'=> 'multipart/form-data'),
));
?>
<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model, 'name', array('maxlength' => 80)); ?>
<?php echo $form->error($model,'name'); ?>
</div><!-- row -->
<div class="row">
<?php echo $form->fileField($model, 'file'); ?>
<?php echo $form->error($model,'file'); ?>
</div><!-- row -->
What am I doing wrong?
Thanks for any help
jayant
(Codesutras)
July 24, 2013, 7:14am
2
[size=2]Could you please try with this code.[/size]
array('file', 'file', 'on' => 'insert',
'allowEmpty' => false,
//'safe'=>true,
'maxSize'=> 512000,
'maxFiles'=> 1,
'mimeTypes' => 'application/msword, text/plain',
'tooLarge'=> 'file cannot be larger than 500KB.',
'wrongMimeType'=> 'Format must be: .doc .txt'
),
jonny
(Jonny)
July 25, 2013, 1:44am
3
Hi Codesutra,
Tried your code, same result
jayant
(Codesutras)
July 25, 2013, 4:32am
4
Its strange.
Can you please show your complete code for model rules?
yureshwar
(Yureshwar)
July 26, 2013, 6:47am
5
What scenario have you given in the controller for the model.
Just add after $model declaration in the controller action as you have defined the validation rule for the scenario insert.
$model->scenario=‘insert’;
Let me know if it has solved your problem
I have a similar problem.
Anyone knows what’s wrong?
drmovi
(Drmovi)
July 26, 2013, 6:31pm
7
ok make sure that yo do the following
1- define the scenario eg:
$model->scenario = 'insert';
2- validate the form eg:
if(isset($_POST['form']){
if($model->Validate()){
//complete process
}
}
jonny
(Jonny)
July 27, 2013, 7:22pm
8
drmovi:
ok make sure that yo do the following
1- define the scenario eg:
$model->scenario = 'insert';
2- validate the form eg:
if(isset($_POST['form']){
if($model->Validate()){
//complete process
}
}
Adding a validate() call after the attributes have assigned seemed to clear up my problem. Although I did think the save() method automatically called the validate() method.