Issue
I need to create a CActiveForm that allows decimal values to be entered into a numberField.
The model rules include setting that attribute's integerOnly value to false, but it seems like the problem is with the form, not the model's validation rules. When I try to submit the form with a decimal value, it tells me to enter a valid value.
The message depends on the browser that I'm using. Safari says, "Enter a valid value." Chrome says, "Please enter a valid value. The two nearest valid values are x and y." where x and y are the two closest integers to what I input.
Has anybody experienced this or does anyone know how to correct it?
Following is code similar to what I have:
FORM
<?php $form = $this->beginWidget('CActiveForm', array(
'action' => Yii::app()->createUrl('person/create', array(
'school' => $school->id
)),
'method' => 'get',
'htmlOptions' => array(
'class' => 'generic-form'
)
)); ?>
<div class="row">
<div class="left field-container one-fourth">
<?= $form->label($person, 'name') ?>
<?= $form->textField($person, 'name') ?>
</div>
<div class="left field-container one-fourth">
<?= $form->label($person, 'height') ?>
<?= $form->numberField($person, 'height') ?>
</div>
<div class="left field-container one-fourth">
<?= $form->label($person, 'age') ?>
<?= $form->numberField($person, 'age') ?>
</div>
<div class="left one-fourth">
<?= TbHtml::submitButton('', array(
'id' => 'submit-create-person',
'class' => 'fa fa-plus generic-button'
)); ?>
</div>
</div>
<?php $this->endWidget() ?>
MODEL
public function rules()
{
return array(
array('school_id', 'required'),
array('name', 'safe'),
array('height', 'numerical', 'integerOnly' => false),
array('age', 'numerical', 'integerOnly'=>true),
);
}
Solution
You can use textField instead of numberField for validation in your form.
Form
<div class="left field-container one-fourth">
<?= $form->label($person, 'height') ?>
<?= $form->textField($person, 'height') ?>
</div>
You get different validation errors on the different browser because of your number input type. All browser has its own validation. you can check by inspect element numberField
is number input type.
If you have Yii 1.1.7 or up version then you can use enableClientValidation to true or false this will disable your browser validation.
Check your Yii version echo Yii::getVersion();
Answered By - Nileshsinh Rathod
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.