CTabView with 2 times the same form

Hi,

I just made a simplest form to try to display model errors at only the tab from where the form was sent.

I used 2 times the same form with 2 differents scenario.

I always sent an empty form (to view validation errors) :

controller :




	public function actionCreate()

	{

		$model=new Foobar(isset($_POST['scenario']) ? $_POST['scenario'] : '');

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

		{

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

			if($model->save())

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

		}

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

			'model'=>$model,

		));

	}



Model:




public function rules()

	{

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

		// will receive user inputs.

		return array(

			array('title', 'required','on'=>'insert'),

			array('title', 'required','on'=>'update'),

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

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

			array('id, title', 'safe', 'on'=>'search'),

		);



View:




<h1>Create Foobar</h1>

    <?php $this->widget('CTabView',array(

    'tabs'=>array(

        'tab1'=>array(

            'title'=>'tab1',

            'view'=>'_form',

            'data'=>array(

                'model'=>$model,

            ),

        ),

        'tab2'=>array(

            'title'=>'tab2',

            'view'=>'_form2',

            'data'=>array(

                'model'=>$model,

            ),

        ),

))); ?>



form:




	<div class="row">

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

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

                <input type="hidden" name="scenario" value="insert">

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

	</div>



form2 (the same with an update scenario):




	<div class="row">

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

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

                <input type="hidden" name="scenario" value="update">

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

	</div>



i get:

[color="#FF0000"]Title cannot be blank.[/color] (@ each tab !)

Do i have to create 2 models (one for each form) or scenarios are enough ? (they seems not enough)

I think the hasErrors() method should accept a scenario parameter, then the following code will be possible:




<?php

if ($this->model->hasErrors(NULL,'my_scenario'):

echo $form->errorSummary($this->model);

endif;?>



I think you could send two model instances to your view, with the scenario already set. If you only send one instance, the potential for confusion is immense. Or have you thought about using CJUITabs? Looks like you could use ajax to load your actions separately.

What do you think about my own answer ?

well, the model will have only one scenario in any case. And if you send only one model instance to your view, it will always have the same scenario!

Additionally, in your controller action, I don’t see where you set the scenario. Shouldn’t you be doing something like this:




if($_POST['scenario'] == 'insert'

   $model->scenario = 'insert'

Here i setted up the scenario:




$model=new Foobar(isset($_POST['scenario']) ? $_POST['scenario'] : '');



Please take a look at:

CActiveRecord->__construct()

It’s the errorSummary() method that should accept a ‘scenario’ parameter instead !

To returns all the errors related to a specified scenario

@mdomba or an admin: what do you think about errorSummary() to handle errors for a specific scenario?

Yii will not be as so optimized if 2 model instances are required for a so simple case.