Yii file upload is not validating file type

2019-08-14 04:19发布

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条回答
放我归山
2楼-- · 2019-08-14 04:52

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.

查看更多
祖国的老花朵
3楼-- · 2019-08-14 05:05

This should work perfectly.

array('barangay_clearance', 'file','types'=>'jpg', 'allowEmpty'=>true, 'on'=>'update', 'on'=>'insert'),
查看更多
登录 后发表回答