yii1 CActiveForm numberfield 不接受十进制值

yii1 CActiveForm numberfield does not accept decimal values

我需要创建一个允许将十进制值输入到 numberField 中的 CActiveForm。

模型规则包括将该属性的 integerOnly 值设置为 false,但问题似乎出在表单上,​​而不是模型的验证规则。当我尝试提交带有十进制值的表单时,它告诉我输入一个有效值。

消息取决于我使用的浏览器。 Safari 说,"Enter a valid value." Chrome 说,"Please enter a valid value. The two nearest valid values are x and y." 其中 x 和 y 是最接近我输入的两个整数。

有没有人遇到过这个问题或者有人知道如何纠正它?

以下代码与我的类似:

表格

<?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() ?>

型号

public function rules()
{
        return array(
                array('school_id', 'required'),
                array('name', 'safe'),
                array('height', 'numerical', 'integerOnly' => false),
                array('age', 'numerical', 'integerOnly'=>true),
        );
}

您可以在表单中使用 textField 而不是 numberField 进行验证。

表格

<div class="left field-container one-fourth">
     <?= $form->label($person, 'height') ?>
     <?= $form->textField($person, 'height') ?>
</div>

由于您的数字输入类型不同,您在不同的浏览器上会收到不同的验证错误。所有浏览器都有自己的验证。您可以通过检查元素检查 numberField 是数字输入类型。

如果您有 Yii 1.1.7 或更高版本,那么您可以使用 enableClientValidation 为 true 或 false 这将禁用您的浏览器验证。

检查你的 Yii 版本 echo Yii::getVersion();