model required field not validating on server side

I have two models say A and B rendering in one form, when i submit the form request going to A controller and validating all A mandatory fields but not B.

My A model is below i am posting only rules.




class Bugs extends CActiveRecord

{

    public function rules()

	{

		return array(

			array('assigned_to, short_desc', 'required'),

			array('assigned_to', 'numerical', 'integerOnly'=>true),

		);

	}

}



My B model is below




class BugsFulltext extends CActiveRecord

{

     public function rules()

	{

		return array(

			array('bug_id, short_desc, comments', 'required'),

			array('bug_id', 'numerical', 'integerOnly'=>true),

			array('short_desc', 'length', 'max'=>255),

			array('comments_noprivate', 'safe'),

			array('bug_id, short_desc, comments, comments_noprivate', 'safe', 'on'=>'search'),

		);

	}

}




and my A controller ie. BugsController goes below





class BugsController extends Controller

{

     public function actionCreate()

	{

		$model=new Bugs;

                $problemdescription=new BugsFulltext;


		if(isset($_POST['Bugs'],$_POST['BugsFulltext']))

		{

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

                        $problemdescription->attributes=$_POST['BugsFulltext'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->bug_id));

		}

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

			'model'=>$model,

                        'problemdescription'=>$problemdescription,

		));

        }

}



and my form is here




<div class="form">


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

	'id'=>'bugs-form',

	'enableAjaxValidation'=>false,

        'enableClientValidation'=>true,

)); 

?>


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


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

        <div class="row">

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

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

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

        </div>

        <div class="row">

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

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

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

        </div>

        <div class="row">

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

            <?php echo $form->textArea($problemdescription,'comments',array('rows'=>4, 'cols'=>45)); ?>

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

        </div>



I am using yii 1.1.7 i enabled client side validation, my textarea field ‘comments’ is validating on client if it is empty, but when i submit the page, on the controller actionID even though it is empty its not giving error message while for the remaining two fields its throwing error on empty.

Could some one help me on this.

Thanks in advance,

Pavan

It’s because you are saving (and validating) only the first model…

Check this wiki article for how to validate both models and only if both validation passes… save both models - http://www.yiiframew…-or-more-models

This article worked fine for me but i added a bit more code to my form which is below




if($model->isNewRecord){

   echo CHtml::textField('savetosession_name').'&nbsp;&nbsp;';

   echo '<input type="submit" name="savetosession" value="Save to Session">';

   echo '<input type="submit" name="create" value="Cteate">';

}else{

   echo '<input type="submit" name="save" value="Save">';

}



Now my textfield ‘savetosession_name’ is not a part of any model, when i click on the ‘Save to Session’ button i would like to validate on the server whether any name is set or not for that i used




if(empty($_POST['savetosession_name'])){

}



but how can i return a error message back to the form specifying the field should not be blank and how can i highlight it in red border.

You can add an attribute to the model for this like


public $savetosession_name;

This way you can add a rule for it… and the error reporting will work like it does for all other model attributes…

In my newly added code i have two submit buttons ‘create’ and ‘savetosession’. I want to validate textfield ‘savetosession_name’ only when ‘savetosession’ button is clicked.

I solved it by myself




in the view i have text field and submit buttons like this


echo $form->textField($model,'savetosession_name',);

echo $form->error($model,'savetosession_name');

echo '<input type="submit" name="savetosession" value="Save to Session">';

echo '<input type="submit" name="create" value="Create">';


in the model i declared variable as 


public $savetosession_name;


in the controller


if(isset($_POST['savetosession'])){

   if(empty($_POST['savetosession_name'])){

         $model->addError('savetosession_name','Save to Session is empty');

   }                    

}



Any custom fields can be validated like above. Is there a better solution than this.