calculated field - confused...

I’m stuck trying to display a calculation on a form. Whats the best way to do this?

Here’s what I’ve got - the ajax validation is working but the salesMargin field isn’t being updated.

model MarginVatForm.php




<?php


/**

 * MarginVatForm class.

 * MarginVatForm is the data structure for keeping

 * margin vat form data. It is used by the 'margin vat' action of 'SiteController'.

 */

class MarginVatForm extends CFormModel

{

	public $purchasePrice;

	public $salePrice;

	

	public $salesMargin;

	

	public function rules()

	{

		return array(

			array('purchasePrice, salePrice, salesMargin', 'calculateMargin'),

			array('purchasePrice, salePrice', 'required'),

			array('purchasePrice, salePrice', 'type', 'type'=>'float'),

			

			array('purchasePrice, salePrice, salesMargin', 'safe'),


		);

	}


	/**

	 * Declares attribute labels.

	 */

	public function attributeLabels()

	{

		return array(

			'purchasePrice'=>'Purchase Price',

			'salePrice'=>'Sale Price',

			'salesMargin'=>'Sales Margin',

		);

	}

	

	public function calculateMargin( $attribute, $params )

	{

		$this->salesMargin=0;

		if( $this->purchasePrice !== null && $this->salePrice !== null )

			$this->salesMargin = $this->salePrice - $this->purchasePrice;

	}


}



Controller




	public function actionMarginVat()

	{

		$model=new MarginVatForm;

				

		// if it is ajax validation request

		if(isset($_POST['ajax']) && $_POST['ajax']==='margin-vat-form')

		{

			echo CActiveForm::validate($model);

			Yii::app()->end();

		}


		$this->render('marginvat',array('model'=>$model));

	}	



view - marginvat.php




<?php

$this->pageTitle=Yii::app()->name . ' - Margin Vat Calulator';

$this->breadcrumbs=array(

	'Margin Vat',

);

?>


<h1>Margin VAT Calculator</h1>


<div class="form">

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

	'id'=>'margin-vat-form',

	'enableAjaxValidation'=>true,

)); ?>


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


	<div class="row">

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

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

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

	</div>


	<div class="row">

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

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

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

	</div>


	<div class="row">

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

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

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

	</div>


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

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




Okay, I can do it with JavaScript but I was kind of hoping I could keep this in the model

within the public function actionMarginVat() controller




	Yii::app()->clientScript->registerCoreScript('jquery');


	Yii::app()->clientScript->registerScript('totalValues',"

		function totalValues()

		{

		var tot = $('#MarginVatForm_salePrice').val() - $('#MarginVatForm_purchasePrice').val();

		$('#MarginVatForm_salesMargin').val( tot );

	}

	", CClientScript::POS_HEAD);


	// This has to be at the end because the elements haven't been created yet

	Yii::app()->clientScript->registerScript('changeTotals', "	

		$('#MarginVatForm_salePrice').change( function(){ totalValues(); } );

		$('#MarginVatForm_purchasePrice').change( function(){ totalValues();} );

	", CClientScript::POS_END);