Model Validations Not Working When Creating Record In Yii

Hi all,

I really don’t know why is it happening my model validations are not working while creating record in yii. doesn’t display any errors . The thing is if any of the required field is empty though it passes to the display page not displaying errors but it doesn’t insert the record as all required field a not filled.

My need is display errors in the same form i.e., validations should not pass if required fields are empty.

validation works with no issues in update, issues with create form

but it inserts the record if all required field are filled.

errors displayed in update are black not red as default by yii … is it due to the extension am using

model rules

[code]

    array('name, category, model, brand, description, price', 'required'),


    array('pimg', 'file','types'=>'jpg','on'=>'create'),


    array('pimg', 'file','types'=>'jpg','on'=>'update', 'allowEmpty'=>true),

[code]

can someone PLEASE tell me how can i achieve this . Thank you

can you please put your form snapshot here?? so that can make us clear about problem… I think this is issue regarding image validation right???

I really don’t know why is it happening my model validations are not working while creating record in yii.

doesn’t display any errors .

The thing is if any of the required field is empty though it passes to the display page not displaying errors

but it doesn’t insert the record as all required field a not filled.

My need is display errors in the same form i.e., validations should not pass if required fields are empty.

validation works with no issues in update, issues with create form

but it inserts the record if all required field are filled.

errors displayed in update are black not red as default by yii … is it due to the extension am using

model rules




            array('name, category, model, brand, description, price', 'required'),

			array('pimg', 'file','types'=>'jpg','on'=>'create'),

			array('pimg', 'file','types'=>'jpg','on'=>'update', 'allowEmpty'=>true),


controller for create


         $model=new controllername;

	   

		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['controllername']))

		{

			$model->attributes=$_POST['controllername'];

			$model->pimg=CUploadedFile::getInstance($model,'pimg');

			$fileName = $model->pimg;

		

		

			if($model->save())

				$model->pimg->saveAs('images/'.$fileName);

				$this->redirect(array('display','id'=>$model->productid));

				

		}


		$this->render('create',array(

	      'model'=>$model,

		));


view

 

      <?php $form=$this->beginWidget('CActiveForm',array(

       'id'=>'form_name',

         'enableAjaxValidation'=>false,

        'htmlOptions'=>array('enctype'=>'multipart/form-data'),

          )); ?>

         

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

  

     

  


	  

  		<?php echo $form->labelEx($model,'name'); ?>

		<?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>60)); ?>

		<?php echo $form->error($model,'name'); ?>

		

		<?php echo $form->labelEx($model,'model'); ?>

		<?php echo $form->textField($model,'model',array('size'=>30,'maxlength'=>30)); ?>

		<?php echo $form->error($model,'model'); ?>

   

  

	  <?php echo $form->labelEx($model,'description'); ?>

		<?php echo $form->textField($model,'description',array('size'=>60,'maxlength'=>256)); ?>

		<?php echo $form->error($model,'description'); ?>

	  

	 

	  

	    <?php echo $form->labelEx($model,'pimg'); ?>

		<?php echo $form->hiddenField($model,'pimg',array('length'=>222)); ?>

        <?php echo $form->fileField($model, 'pimg',array('id'=>'imgInput',)); ?>

		

		<?php echo $form->error($model,'pimg'); ?>

  

   

  

		<?php echo $form->labelEx($model,'category'); ?>

		<?php echo $form->dropDownList($model,'category',$model->getCat()); ?>

		<?php echo $form->error($model,'category'); ?>

	

	

	<?php echo $form->labelEx($model,'brand'); ?>

		<?php echo $form->textField($model,'brand',array('size'=>30,'maxlength'=>30)); ?>

		<?php echo $form->error($model,'brand'); ?>

	

		<?php echo $form->labelEx($model,'price'); ?>

		<?php echo $form->textField($model,'price'); ?>

		<?php echo $form->error($model,'price'); ?>

		

	

		

	

  	

 

     <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

   

  

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




can someone PLEASE tell me how can i achieve this . Thank you

ya I do know that but the thing is … am uable to figure out :(

In create and update same form is render???

why are you using following line??




  <?php echo $form->hiddenField($model,'pimg',array('length'=>222)); ?>



thank you … I got the answer

its just that code




 if($model->save())

                                $model->pimg->saveAs('images/'.$fileName);

                                $this->redirect(array('display','id'=>$model->productid));

                                

 must be


if($model->save())

                        

{        $model->pimg->saveAs('images/'.$fileName);

         $this->redirect(array('display','id'=>$model->productid));

           }