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,
    ));
}

2 Answers 2

5

This should work perfectly.

array('barangay_clearance', 'file','types'=>'jpg', 'allowEmpty'=>true, 'on'=>'update', 'on'=>'insert'),
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. It worked. Don't know why it worked on 'update' even though I'm not on any 'update' scenario. Thanks again :)
I am happy that, this answer worked for you. You can set this answer as your accepted answer.
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 :)
This worked on 'update' because the rule has (wrongly) 'on' twice and the 2nd overwrite the 1st.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.