Model Rule Access

I used two tables in my application. For creation of 1st table, the 1st create form include the 2nd table form fields and data saved into two tables. But in the validation only 1st table form fields are validated. How can include/access the 2nd table model rules in the 1st model.?

In your controller’s create method, you should use both models as well. See these two wikis:

Performed like this tutorial but the validation not occurs. Checking fields are NULL in 2nd model form fields not happening.

Can you post the code from your action and the rules for each model?

In controller- User model

public function actionCreate()

{


	$model=new User;





	// Uncomment the following line if AJAX validation is needed


	$this->performAjaxValidation($model);





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


	{


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


		if($model->save())


                    {


                        


                        $modelBuyer = new Buyer;


                        $modelBuyer->attributes = $_POST['Buyer'];


                        $modelBuyer->user_id= $model->user_id;


                        if ($modelBuyer->save())


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


	}


            }


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


		'model'=>$model,


	));


            


}

.

.

.

Model Rules- User Model

public function rules()

{


        


// NOTE: you should only define rules for those attributes that


	// will receive user inputs.


	return array(


		array('first_name, lastname, email, password, country, hear_aboutus', 'required', ),


                    array('first_name,email','unique','message'=>'{attribute} already exist '),	


                    array('first_name, lastname, email, password, country', 'length', 'max'=>25),


		array('hear_aboutus', 'length', 'max'=>20),


                    array('email','email','checkMX'=>true),


                    array('password', 'compare', 'compareAttribute'=>'repeat_password'),


		// The following rule is used by search().


		// Please remove those attributes that should not be searched.


		array('user_id, first_name, lastname, email, password, country, hear_aboutus', 'safe', 'on'=>'search'),


	);


}

Model Rules- Buyer

public function rules()

{


	// NOTE: you should only define rules for those attributes that


	// will receive user inputs.


	return array(


		array('user_id, industry, position', 'required'),


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


		array('industry, position', 'length', 'max'=>50),


		// The following rule is used by search().


		// Please remove those attributes that should not be searched.


		array('user_id, industry, position', 'safe', 'on'=>'search'),


	);


}

The buyer model form fields used in the User creation form. Now the validation for form fields are empty, the user model form fields are validated but the buyer fields not validated. How include the buyer model rules in user

model?

You don’t seem to be passing your Buyer instance out to your view, which would explain why you’re not seeing any validation errors against it. You must use the same instance of Buyer in both the action and the view.

Please post your view code.

And please format your code: use the [font=“Courier New”]<>[/font] button in the editor toolbar, or if you’re on mobile, like this:


[code] /* put your code between these tags */ [ /code]

(remove the space between [ and /)

or


[php]/* put your PHP code between these tags */[ /php]

(remove the space between [ and /)


<div class="form">


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

	'id'=>'user-form',

	'enableAjaxValidation'=>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,Yii::t('app','first_name')); ?>

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

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

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,Yii::t('app','lastname')); ?>

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

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

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,Yii::t('app','email')); ?>

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

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

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,Yii::t('app','password')); ?>

		<?php echo $form->passwordField($model,'password',array('size'=>50,'maxlength'=>50)); ?>

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

	</div>

        <div class="row">

                <?php echo $form->labelEx($model,Yii::t('app','Retype password')); ?>

                <?php echo $form->passwordField($model,'password',array('maxlength'=>40)); ?>

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

        </div>

        

	<div class="row">

		<?php echo $form->labelEx($model,Yii::t('app','country')); ?>

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

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

	</div>


	


        <div class="row">

                <?php echo $form->labelEx(Buyer::model(), Yii::t('app','industry')); ?>

                <?php echo $form->textField(Buyer::model(), 'industry', array('size' => 50, 'maxlength' => 50)); ?>

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

        </div>   

         <div class="row">

                <?php echo $form->labelEx(Buyer::model(), Yii::t('app','position')); ?>

                <?php echo $form->textField(Buyer::model(), 'position', array('size' => 50, 'maxlength' => 50)); ?>

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

        </div>  

	<div class="row buttons">

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

	</div>


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


</div><!-- form -->

As I said, you should be using the Buyer instance that you were manipulating in the action, rather than referencing Buyer::model() each time.

how can i solve this?

In the action, something like this:




$buyer = new Buyer;

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

    $buyer->attributes = $_POST['Buyer'];


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

{

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

    if($model->save())

    {

        $buyer->user_id= $model->user_id;

        if ($buyer->save())

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

    }

}

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

    'model'=>$model, 'buyer'=>$buyer,

));



In the view, replace every reference to Buyer::model() with $buyer.

Realistically, you should probably wrap your model updates in a transaction to prevent updating one table and then encountering an error with the second table.