1

I am validating the file type to be uploaded. The validation rule does not seem to work. I only want to accept jpg files but when I try putting pdf files, it still accepts it and does not give any error. Please help. I don't know what I'm doing wrong.

View:

<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'document-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'clientOptions'=>array('validateOnSubmit'=>true), //This is very important
'htmlOptions'=>array('enctype'=>'multipart/form-data'),)); ?>

<p class="note">Fields with <span class="required">*</span> are required.</p>

<?php echo $form->errorSummary($model); ?>

<div class="row">
    <?php echo $form->labelEx($model,'barangay_clearance'); ?>
    <?php echo $form->fileField($model,'barangay_clearance'); ?>
    <?php echo $form->error($model,'barangay_clearance'); ?>
</div>

<div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>

<?php $this->endWidget(); ?>

Model:

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('barangay_clearance', 'file', 'allowEmpty'=>true, 'types'=>'jpg', 'message'=>'Jpg files only', 'on'=>'insert'),
        array('barangay_clearance, barangay_status, zoning_clearance, zoning_status, sanitary_clearance, sanitary_status, occupancy_permit, occupancy_status, fire_safety, fire_safety_status', 'safe'),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('document_id, business_id, barangay_clearance, barangay_status, zoning_clearance, zoning_status, sanitary_clearance, sanitary_status, occupancy_permit, occupancy_status, fire_safety, fire_safety_status', 'safe', 'on'=>'search'),
    );
}

Controller: (Note: I haven't really changed the controller yet except for activating the ajaxvalidation function and creating an instance of another model)

public function actionCreate($id)
{
    $model=new Document;
    $busModel = Business::model()->findByPk($id);

    // Uncomment the following line if AJAX validation is needed
    $this->performAjaxValidation($model);

    if(isset($_POST['Document']))
    {
        $model->attributes=$_POST['Document'];
        if($model->save())
            $this->redirect(array('view','id'=>$model->document_id));
    }

    $this->render('create',array(
        'model'=>$model,
        'busModel'=>$busModel,
    ));
}
Sam R.
  • 16,027
  • 12
  • 69
  • 122
swswsw
  • 33
  • 1
  • 5

2 Answers2

5

This should work perfectly.

array('barangay_clearance', 'file','types'=>'jpg', 'allowEmpty'=>true, 'on'=>'update', 'on'=>'insert'),
Mohit Bhansali
  • 1,725
  • 4
  • 20
  • 43
  • Thanks. It worked. Don't know why it worked on 'update' even though I'm not on any 'update' scenario. Thanks again :) – swswsw May 05 '13 at 10:07
  • I am happy that, this answer worked for you. You can set this answer as your accepted answer. – Mohit Bhansali May 06 '13 at 09:59
  • Oh okay. There I accepted it now. Sorry I am new here in stackoverflow so I didn't know that there was that option to accept an answer. By the way, thanks again :) – swswsw May 07 '13 at 07:48
  • This worked on 'update' because the rule has (wrongly) 'on' twice and the 2nd overwrite the 1st. – sdlins Nov 05 '13 at 00:56
0

You didn't assign any scenario to model. If model you use in form widget is instance of Document then this:

$model=new Document;

has to be replaced with this:

$model=new Document('insert');

since rule declaration

array('barangay_clearance', 'file', 'allowEmpty'=>true, 'types'=>'jpg', 'message'=>'Jpg files only', 'on'=>'insert'),

says that it will be applied only on "insert" scenario.

I hope it helps.

Ruslan Polutsygan
  • 4,401
  • 2
  • 26
  • 37