Yii Framework Forum: CActiveForm numberfield - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CActiveForm numberfield CActiveForm numberfield does not accept decimal values Rate Topic: -----

#1 User is offline   hutch90 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 14-February 18

Posted 14 February 2018 - 07:24 PM

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),
	);
}

0

#2 User is offline   hutch90 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 14-February 18

Posted 15 February 2018 - 11:31 AM

I have solved it by using a textField instead, like this:

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

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users