Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

File Validation

$
0
0
I am using a file field for creating a address model. The file will support extensions( png, gif, jpeg). But when i select another extension file it it shows "field cannot be blank" error.

Address model
public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name', 'required'),
			array('name, address', 'length', 'max'=>55),
                        array('address', 'file', 'types'=>'jpg, jpeg, png','allowEmpty'=>false),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, address', 'safe', 'on'=>'search'),
		);
	}


Controller action
public function actionCreate()
	{
		$model=new Address;

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

		if(isset($_POST['Address']))
		{
                    $model->attributes=$_POST['Address'];
                       $up= CUploadedFile::getInstance($model,'address');  
                       $model->address=$up;
                       $model->validate();
                       if($model->save())
                           {
                       
                        $up->saveAs($_SERVER['DOCUMENT_ROOT'].Yii::app()->request->baseUrl.'/css/'.$up);  // image will uplode to rootDirectory/banner/
                        $this->redirect(array('view','id'=>$model->id));
                           }
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}



_form.php
<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'address-form',
	'enableAjaxValidation'=>true,
        'htmlOptions' => array('enctype' => 'multipart/form-data'),
        'clientOptions'=>array('validateOnSubmit'=>true),
)); ?>

	<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,'name'); ?>
		<?php echo $form->textField($model,'name',array('size'=>55,'maxlength'=>55)); ?>
		<?php echo $form->error($model,'name'); ?>
	</div>

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

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

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

</div>&lt!-- form -->



1. When i am using form without popup dialog box
when i change 'enableAjaxValidation'=>true, to false the validation is correct - "file format error".

2. When i am using popup dialog box for creating a model
when i change 'enableAjaxValidation'=>true, to false the form window is closed without showing error.
when 'enableAjaxValidation'=>true, - the form will show "cannot be blank error"

I want to show file validation error in popup dialog box.
How it solve?
Thanks.

Viewing all articles
Browse latest Browse all 18717

Trending Articles