CHtml::submitButton() parameters

Is there a way that I could change the parameters of the CHtml::submitButton()?

I have this




	<div class="row buttons">

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

	</div>



in my _form. I am recycling or reusing the form but in another controller. Is there any way to address the submitButton to another controller? I am like copy-pasting the actionCreate controller of that form with my new controller except that now it has multiple parameters. Any help please.

[color="#006400"]/* Moved from 2.0 forum to 1.1 */[/color]

Please check the "Working with Forms" sections of the guide.

http://www.yiiframework.com/doc/guide/1.1/en/form.overview

I usually don’t “reuse” nor “recycle” a form when the model is different. It’s safer and easier to write a dedicated form for each model.

It’s not the submit button but the form that specifies the action.

And what do you mean by "multiple parameters"?

What i mean is that they are on the same model since the forms only affect one table in the database. Okay here is my scenario, First I have a form for the user (staff) to allow a customer purchase an item/product. It works well. Now I thought of another option of "what if" the staff will alter the item purchased of the customer?

I cannot use the actionUpdate because in this second form, I have to receive and identify the "type" of the product the staff is altering. I am new to yii and I think I cannot be able to add extra parameters in the actionUpdate controller and implement it in the same form of actionCreate. What I did is that I created a new Controller which is so much alike the actionCreate controller. and a new _form in that case. My problem now is that everytime I clicked the "Create" button in the new form, it marks all the field ERROR (i.e: Price cannot be blank) even if I did put a price in the textfield.

my actionCreate controller:




	public function actionCreate($id)

	{

		$model=new ReleaseItem;

		$model->contract_id=$id;

		// Uncomment the following line if AJAX validation is needed

		 $this->performAjaxValidation($model);


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

		{

			$_POST['ReleaseItem']['tot_price'] = $_POST['ReleaseItem']['price']*$_POST['ReleaseItem']['quantity'];

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

			if($model->save())

			{

				$model->updateBalance($id);

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

			}

		} 


		if (Yii::app()->request->isAjaxRequest) {

			Yii::app()->clientScript->scriptMap['jquery.js'] = false;

			yii::app()->clientScript->scriptMap['jquery-ui.min.js'] = false;

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

			'model'=>$model,'contr_id'=>$id

		),false,true);}

		

		

	}



my default form from CRUD (_form)




<?php

/* @var $this ReleaseItemController */

/* @var $model ReleaseItem */

/* @var $form CActiveForm */

?>


<div class="form">


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

	'id'=>'release-item-form',

	// Please note: When you enable ajax validation, make sure the corresponding

	// controller action is handling ajax validation correctly.

	// There is a call to performAjaxValidation() commented in generated controller code.

	// See class documentation of CActiveForm for details on this.

	'enableAjaxValidation'=>true,

	'clientOptions' => array('validateOnSubmit'=>true),

)); ?>


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


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


<div class="row">

		

		<?php echo $form->textField($model,'contract_id',array('style'=>'display:none;')); ?>

		

	</div>


	<div class="row">

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

		<?php echo $form->dropDownList($model,'product_id', $model->getProducts()) ?>

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

	</div>


	<div class="row">

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

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

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

	</div>


	<div class="row">

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

		<?php Yii::import('application.extensions.EJuiDateTimePicker.EJuiDateTimePicker');

		

    		$this->widget('application.extensions.EJuiDateTimePicker.EJuiDateTimePicker',array(

        		'model'=>$model, //Model object

        		'attribute'=>'date', //attribute name

    			//'dateFormat'=>'dd-mm-yy',

    			

        		'options'=>array(

        		

        		'dateFormat'=>'yy-mm-dd',

   				 ) // jquery plugin options

    		));

		?>	

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

	</div>


	<div class="row">

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

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

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

	</div>


	<div class="row">

		

		<?php echo $form->textField($model,'tot_price',array('style'=>'display:none;')); ?>

		

	</div>


	<div class="row">

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

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

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

	</div>


	<div class="row buttons">

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

	</div>


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


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



my new controller (taken from actionController)




	public function actionReplacecasket($contr, $i_release)

	{

		$model=new ReleaseItem;

		$model->contract_id=$contr;

		// Uncomment the following line if AJAX validation is needed

		$this->performAjaxValidation($model);

		

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

		{

			if($model->save())

			{

				//$model->updateBalance($id);

				$this->redirect(array('servicecontract/admin'));

			}

		}

		

	

		if (Yii::app()->request->isAjaxRequest) {

			Yii::app()->clientScript->scriptMap['jquery.js'] = false;

			yii::app()->clientScript->scriptMap['jquery-ui.min.js'] = false;

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

					'model'=>$model,'contr_id'=>$contr, 

			),false,true);}


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

					'model'=>$model,'contr_id'=>$contr,

			),false,true);

	

	}



and finally my new form., which is clearly not working properly (_formreplace.php)




<?php

/* @var $this ReleaseItemController */

/* @var $model ReleaseItem */

/* @var $form CActiveForm */

?>


<div class="form">


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

	'id'=>'release-item-form',

	// Please note: When you enable ajax validation, make sure the corresponding

	// controller action is handling ajax validation correctly.

	// There is a call to performAjaxValidation() commented in generated controller code.

	// See class documentation of CActiveForm for details on this.

	'enableAjaxValidation'=>true,

	'clientOptions' => array('validateOnSubmit'=>true),

)); ?>


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


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


<div class="row">

		

		<?php echo $form->textField($model,'contract_id',array('style'=>'display:none;')); ?>

		

	</div>

	


	<div class="row">

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

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

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

	</div>


	<div class="row">

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

		<?php echo $form->dropDownList($model,'product_id', $model->getCasket()) ?>

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

	</div>


	<div class="row">

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

		

		<?php echo $form->textField($model,'quantity', array('value'=>'1', 'readOnly'=>'true')); ?>

		

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

	</div>


	<div class="row">

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

		<?php Yii::import('application.extensions.EJuiDateTimePicker.EJuiDateTimePicker');

		

    		$this->widget('application.extensions.EJuiDateTimePicker.EJuiDateTimePicker',array(

        		'model'=>$model, //Model object

        		'attribute'=>'date', //attribute name

    			//'dateFormat'=>'dd-mm-yy',

    			

        		'options'=>array(

        		

        		'dateFormat'=>'yy-mm-dd',

   				 ) // jquery plugin options

    		));

		?>	

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

	</div>


	<div class="row">

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

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

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

	</div>


	<div class="row">

		

		<?php echo $form->textField($model,'tot_price',array('style'=>'display:none;')); ?>

		

	</div>


	<div class="row">

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

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

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

	</div>


	<div class="row buttons">

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

	</div>


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


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



Sorry for the long codes. I really need help. Thanks.

Are you trying to create/update/replace in a popup dialog?

Then you have to specify the action property of the CActiveForm explicitly.

http://www.yiiframework.com/doc/api/1.1/CActiveForm#action-detail




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

    'id'=>'release-item-form',

    'action' => array('relase-item/create', 'id' => $id),

    // or 'action' => array('relase-item/update', 'id' => $id),

    // or 'action' => array('relase-item/replacecasket', 'contr' => $contr, 'i_release' => $i_release),

    'enableAjaxValidation'=>true,

    'clientOptions' => array('validateOnSubmit'=>true),

)); ?>


// Maybe you can pass "$action" parameter from "create", "update" and "createreplace" view.

// 'action' => $action,



Otherwise the form will be submitted to the current url of the page.

Good God! This is why I love Yii and the people around it. You guys are so amazing! One thing, the only hindrance I am getting now is that in my separate form, i am including an administrative password in the alteration for security purposes. and with that, I guess the error comes from the save()… since ($model,password) is not in the database table or AR of the model. Am i right? I can’t perform save() correctly. And i want to include the password textfield only in that form (the alteration form). Please. How can i possibly do it. And oh thank you so much for the help!

You can declare an additional attribute in an AR model. It doesn’t need to have a corresponding column in the db table. And you can use “scenario” to specify whether it should be validated or not for a certain situation.

http://www.yiiframework.com/doc/guide/1.1/en/form.model#declaring-validation-rules