Calculation on textfield like excel


I was wondering whether there is a simple way to add a capability to the textfield to calculate a simple calculation for example: 500/2 and the model will save it as ‘250’ not ‘500/2’. Like in excel, if we put ‘=’ it will calculate the textfield.

Thanks in advance.

I am not clear on the actual challenge you are facing.

From my understanding…You want the user to:

Scenario A:

Enter (500) into field1, But you want (250) saved into field1

or do you mean?

Scenario B:

Enter (500) into field1 and (2) into field2, and the result of (250) saved into field3

This is how I would approach this task…

  1. Generate the models for db as usual.

  2. Then add the additional calculation purpose two fields to the model, with rules etc. (note you will need to add the view for the extra fields)

  3. Collect your fields in view, perform the calculation logic in model or controller, assigning the total output to the field to be saved.

Calc Model

class Calc extends CActiveRecord


        public $field1; // not in db

        public $field2; // not in db



        public function rules()


		return array(

			array('field1, field2', 'required'),

			array('field1, field2, total', 'numerical', 'integerOnly'=>true),

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





class CalcController extends Controller




        public function actionCreate()


		$model=new Calc;




                        $model->total=$model->field1 / $model->field2;












<div class="form">

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



)); ?>

	<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,'field1'); ?>

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

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


	<div class="row">

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

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

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



	<div class="row buttons">

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


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

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

If there are many fields to be calculated, I would build a separate model for collection purpose, then perform total calculation and assign total to class::model->field that will save to db. (Tip: See wiki for collecting two models for a single form).

Hi Seal,

Thank you for the quick reply.

My situation is like this, I have a purchase form with some details, in the purchase_detail, I have a field called unitPrice. Sometimes, the unit in the manual form and in the yii form is different. Currently, I need to use a separate calculator to enter the price. for example in manual form is in dozen but in yii, it is in piece. Hence, I need to be able to enter "=500/12" in the unitPrice field and it will automatically calculated as "42" when saved into the database, like in excel.

Thanks a lot