Creating a custom input widget with CInputWidget

Hello,

Recently upgraded to the latest Yii 1.1.9 and wanted to share a little tweak that had to be made in order for the custom range input field to work…

In protected/components/RangeInputField.php there’s


class RangeInputField extends CInputWidget

{

	public $attributeFrom;

	public $attributeTo;

	public $nameFrom;

	public $nameTo;

	public $valueFrom;

	public $valueTo;

	

	function run()

	{

		if($this->hasModel())

		{

			echo CHtml::activeTextField($this->model, $this->attributeFrom);

			echo ' → ';

			echo CHtml::activeTextField($this->model, $this->attributeTo);

		}

		else {

			echo CHtml::textField($this->nameFrom, $this->valueFrom);

			echo ' → ';

			echo CHtml::textField($this->nameTo, $this->valueTo);

		}

	}

}



the check if($this->hasModel()) uses CInputWidget::hasModel which contains


return $this->model instanceof CModel && $this->attribute!==null;

but we lack the field $attribute in RangeInputField and the check fails, so replacing


if(this->hasModel)

with


if($this->model instanceof CModel)

makes the code to work as intended… Hope this helps.

I find the book very useful and well arranged so far. Thanks a lot Макаров :)

Maybe not need to change the Framework, but append follow code in run() but before if($this->hasModel()):




if($this->attributeFrom && $this->attributeTo) {

            $this->attribute = TRUE;

}




or?