Textfield Name

Hello i´m new in yii i have the next code for sum two values:

My problem is if a set the property ‘name’ in the textField the Form send null values to the database please help !!!

<?php echo $form->textField($model,‘var1’,array(‘value’=>0, ‘name’=>‘var1’,‘onChange’=>‘js:sum()’));?>

<?php echo $form->error($model,‘var1’); ?>

<?php echo $form->textField($model,‘var2’,array(‘value’=>0, ‘name’=>‘var2’,‘onChange’=>‘js:sum()’));?>

<?php echo $form->error($model,‘var2’); ?>

<script>

        function sum(){


            var num1 = parseInt(document.getElementById('total_Pzas_Ok1').value);


            var num2 = parseInt(document.getElementById('total_Pzas_NO_Ok').value);


            var result = num1 + num2;


            if (result &lt; 0){


                document.getElementById('val').value = 0;


            }else{


                document.getElementById('val').value = result;


            }


        }


    &lt;/script&gt;

You can’t set manually name property for form data like- input, buttons and all things. if your set manually then your save function doesn’t recognize the form post data in your action controller.

[color="#006400"]/* Moved from "Miscellaneous" to "General Discussion for Yii 1.1.x" */[/color]

CActiveForm::textField automatically generates the ‘name’ property for you that is compatible with the way the controller receives the input values from $_POST. The generated ‘name’ property is usually in the form of ‘ModelName[attribute_name]’ … check the HTML generated by Yii.

But I would recommend you to use CHtml::activeName to get the name, because it is the method that CActiveForm::textField uses to generate the name.




var num1 = parseInt(document.getElementByName('<?php echo CHtml::activeName($model, 'val1'); ?>').value);