More then one form on the same page using the same model

So I have multiple forms on one page with multiple submit buttons (see attachment). When update the “Change Email” section and click on “Change Email”, the button triggers the event to change the password instead. Likewise, If I were to update the “Personal Information”, and click on “Update”, it triggers the event to change the password instead. I’m having trouble with the submit buttons performing the right action. All three submit buttons are performing “Change Password” instead.

protected/controllers/UsersController.php


   public function actionUpdate($id)

    {

        $model=$this->loadModel($id);


        // Uncomment the following line if AJAX validation is needed

        $this->performAjaxValidation($model);


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

        {

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


            if($model->new_password !== '')

                $model->setScenario('changePassword');


            if($model->validate())

            {

                if($model->new_password !== '')

                    $model->password = $model->encryptPass($model->new_password);


                // Code to change profile here


                // Code to change email here


                if($model->save(false))

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


            }


        }


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

            'model'=>$model,

        ));

    }



protected/views/users/update.php




<?php

$this->breadcrumbs=array(

	'Users'=>array('index'),

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

	'Update',

);


$this->menu=array(

	array('label'=>'List Users', 'url'=>array('index')),

	array('label'=>'Create Users', 'url'=>array('create')),

	array('label'=>'View Users', 'url'=>array('view', 'id'=>$model->user_id)),

	array('label'=>'Manage Users', 'url'=>array('admin')),

);

?>


<h1>Update Users <?php echo $model->user_id; ?></h1>

<div class="form">




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


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

	'id'=>'changepassword',

	'enableAjaxValidation'=>true,

)); ?>

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

	<fieldset><legend>Change Password</legend>

		<div class="row">

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

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

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

		</div>

		<div class="row">

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

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

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

		</div>

		<div class="row buttons">

			<?php echo CHtml::submitButton('Change Password'); ?>

		</div>

	</fieldset>


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


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

	'id'=>'changeemail',

	'enableAjaxValidation'=>true,

)); ?>

	<fieldset><legend>Change Email</legend>

		<div class="row">

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

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

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

		</div>


		<div class="row">

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

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

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

		</div>

		<div class="row buttons">

			<?php echo CHtml::submitButton('Change Email'); ?>

		</div>

	</fieldset>

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




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

	'id'=>'changeprofile',

	'enableAjaxValidation'=>true,

)); ?>

	<fieldset><legend>Personal Information</legend>

		<div class="row">

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

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

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

		</div>


		<div class="row">

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

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

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

		</div>


		<div class="row">

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

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

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

		</div>


		<div class="row">

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

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

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

		</div>


		<div class="row">

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

			<?php echo $form->textField($model,'homephone',array('maxlength'=>12)); ?>(Format: XXX-XXX-XXXX)

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

		</div>




		<div class="row">

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

			<?php echo $form->textField($model,'cellphone',array('maxlength'=>12)); ?>(Format: XXX-XXX-XXXX)

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

		</div>


		<div class="row">

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

			<?php echo $form->dropDownList($model,'dobYear',Users::model()->rangeAssoc(2011,1900), array('prompt'=>'')); ?>

			<?php echo $form->dropDownList($model,'dobMonth',Users::model()->getMonthOptions(), array('prompt'=>'')); ?>

			<?php echo $form->dropDownList($model,'dobDay',Users::model()->rangeAssoc(1, 31), array('prompt'=>'')); ?>

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

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

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

		</div>


		<div class="row">

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

			<?php echo $form->dropDownList($model,'gender',Users::model()->getGenderOptions(), array('prompt'=>'')); ?>

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

		</div>


		<div class="row">

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

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

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

		</div>


		<div class="row">

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

			<?php echo $form->dropDownList($model,'carrier',Users::model()->getCarrierOptions(), array('prompt'=>'')); ?>

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

		</div>

		<div class="row buttons">

			<?php echo CHtml::submitButton('Update'); ?>

		</div>

	</fieldset>

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

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



You can use multiple action with the same view.

Just set the property action of CActive form for make each form to post to his proper action.

Another option is to use an hidden field in each form, but I think that multiple action is more clean.

Would you please show me a syntax to set an action in CActive form…here i am struggling with syntax of this…i have a same issue in my app… multiple form on same page.with different action.

Maybe this is another option for you:

Two forms on the same page and scenario validation

(zaccaria: I’ve used my secret bookmark powers again ;) )

mmmm… unbelivable power…

I think that there are 2 schools: multple actions or tabuar input. I’d say that they are more or less equivalent.

i think multiple action handling is better approach to perform this kind of task. ;)