Yii Framework Forum: Creating a custom input widget with CInputWidget - Yii Framework Forum

Jump to content

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

Creating a custom input widget with CInputWidget Chapter 4, Page 118

#1 User is offline   yasen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 29-October 11
  • Location:Sofia, Bulgaria

Posted 14 January 2012 - 02:57 AM

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 Макаров :)
0

#2 User is offline   Mikewolf 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 04-January 12

Posted 25 January 2012 - 03:49 PM

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?
Newbee of Yii
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