Fields of different AR classes in one form, one validation

Hey guys!

What is the way to have files from two AR models.

Posted Image

I filled these fields, but I suppose Yii can't remember their value, because they are from another AR class.



<?php echo CHtml::form(); ?>





<?php echo CHtml::errorSummary($userfirms); ?>





<div class="simple">


<?php echo CHtml::activeLabelEx($usersfirms,'USERNAME'); ?>


<?php echo CHtml::activeTextField($usersfirms,'USERNAME'); ?>


</div>


<div class="simple">


<?php //echo CHtml::activeLabelEx($userfirms,'CITY_ID'); ?>


<?php //echo CHtml::activeTextField($userfirms,'CITY_ID'); 








?>


</div>


<div class="simple">








<?php echo CHtml::activeLabelEx($userfirms,'CITY_ID'); ?>


<?php echo CHtml::activeDropDownList($userfirms,'CITY_ID', CHtml::listData(Cities::model()->findAll(


In the code above I’ve included only the field “USERNAME”, because the solution is for the others too :). What is the access to these fields? Where should I put validation rules and so on, should I create a separate controller or something?

Penko

What are your two models? I only saw $userfirms.

Yes, because I didn't post it.



<?php





class users extends CActiveRecord


{


	/**


	 * Returns the static model of the specified AR class.


	 * @return CActiveRecord the static model class


	 */





	


	//public $defaultAction='action';


	


	public static function model($className=__CLASS__)


	{


		return parent::model($className);


	}





	//public function action() {


		


		//$users->type=$type;


		/*$this->dbConnection->createCommand(


            'INSERT INTO `users` (USER_ID) VALUES('.$post->title.')')->execute();*/


		//$users->save();


		//$post->save();


	//}


	


	/**


	 * @return string the associated database table name


	 */


	public function tableName()


	{


		return 'users';


	}





	/**


	 * @return array validation rules for model attributes.


	 */


	public function rules()


	{


		return array(


		);


	}


	


	/**


	 * @return array relational rules.


	 */


	public function relations()


	{


		return array(


		);


	}





	/**


	 * @return array customized attribute labels (name=>label)


	 */


	public function attributeLabels()


	{


		return array(


		);


	}


}


The field 'USERNAME' is from this class "users". I have made a mistake to specify it from "userfirms", but if I specify "users" in its declaration, I am getting an error. What is the way to handle field data from the two different models.

If you have two or more model instances (from different AR classes), you can build the view like you do with a single model. You just need to use different variable names at different places. Remember to pass to the view the needed variables. In your action, you need to assign $_POST to every model's attributes.

Qiang,

what do you mean by passing variables to the view. Do you mean to add argument to



<?php $this->renderPartial('_form', array(


	'post'=>$post,


	'update'=>false,


)); ?>


for each of the fields, who are not from the current AR model? In our case they are USERNAME, PASSWORD, PASSWORD REPEAT, EMAIL.

Actually, the entire problem is in handling their data and validation as Yii can't understand them properly and it is logical, because they are not from the current class.

Sorry, I didn't get fully about making one class behave as a single model. Also, the part for $_POST. Could you give a little example for a view, that consists one field from one model and another field, from a different class. Use mine so that is is easier for you, if you want.

It's just unclear to me what access I do have for these fields as they are from another class and actually their validation. Should I put it in the main class's validation method in the model.

I only skimmed through this thread but I believe this is what he means:

<?php


$this->renderPartial('_form', array(


   'model1'=>$model1,


   'model2'=>$model2,


)); ?>





//view


<?php echo CHtml::activeTextField($model1,'field in model1'); ?>





<?php echo CHtml::activeTextField($model2,'field in model2'); 





Yes, probably. I will try it tomorrow, because it's 23:44 here and will tell you what happens.