resolveNameID()

Hello everybody.

I’m testing some class code.

I do not know how resolveNameID() gets the name and id of a widget or class?.

I try test this code:




public function run()

   {

      list($name, $id)=$this->resolveNameID();


      $dir = dirname(__FILE__).DIRECTORY_SEPARATOR.'jquery';

      $baseUrl =Yii::app()->getAssetManager()->publish($dir);


      $cs = Yii::app()->getClientScript();

      $cs->registerCoreScript('jquery');

      $cs->registerScriptFile($baseUrl.'/js/colorpicker.js');

      $cs->registerScriptFile($baseUrl.'/js/eye.js');

      $cs->registerScriptFile($baseUrl.'/js/utils.js');

      $cs->registerCssFile($baseUrl.'/css/colorpicker.css');

      	

      $options = $this->jsOptions();


      $js_effects = '';

      if (($this->mode != 'flat') && ($this->fade || $this->slide || $this->curtain)) {

         $fi = $fo = $si = $so = $ci = $co = '';

         if ($this->fade) {

            $fi = "$(colpkr).fadeIn({$this->timeFade});";

            $fo = "$(colpkr).fadeOut({$this->timeFade});";

         }

         if ($this->slide) {

            $si = "$(colpkr).slideDown({$this->timeSlide});";

            $so = "$(colpkr).slideUp({$this->timeSlide});";

         }

         if ($this->curtain) {

            $ci = "$(colpkr).show({$this->timeCurtain});";

            $co = "$(colpkr).hide({$this->timeCurtain});";

         }

         

         $js_effects =<<<EOP

onShow: function (colpkr) {

   {$fi}

   {$si}

   {$ci}

   return false;

},

onHide: function (colpkr) {

   {$fo}

   {$so}

   {$co}

   return false;

},

EOP;

      }


      switch ($this->mode) {

         case 'textfield':

            $this->htmlOptions['id'] = $id;

            $this->htmlOptions['size'] = !isset($this->htmlOptions['size']) ? 6 : $this->htmlOptions['size'];

            $this->htmlOptions['maxlength'] = !isset($this->htmlOptions['maxlength']) ? 6 : $this->htmlOptions['maxlength'];

         if($this->hasModel())

            $html = CHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions);

         else

            $html = CHtml::textField($name, $this->value, $this->htmlOptions);

            $js =<<<EOP

$('#{$this->htmlOptions['id']}').ColorPicker({

   {$js_effects}

	onSubmit: function(hsb, hex, rgb) {

		$('#{$this->htmlOptions['id']}').val(hex);

	},

	onBeforeShow: function() {

		$(this).ColorPickerSetColor(this.value);

	},

   onChange: function(hsb, hex, rgb) {

      $('#{$this->htmlOptions['id']}').val(hex);

   }

})

.bind('keyup', function(){

	$(this).ColorPickerSetColor(this.value);

});

EOP;

            $cs->registerScript(get_class($this).'_'.$id, $js);

            echo $html;

            break;


         case 'flat':

            $html =<<<EOP

<div id="{$id}"></div>

EOP;

            $js =<<<EOP

$('#{$id}').ColorPicker({$options});

EOP;

            $cs->registerScript(get_class($this).'_'.$id, $js);

            echo $html;

            break;


         case 'selector':

            if (empty($this->selector)) {

               throw new CException(Yii::t('EColorPicker','A selector must be specified.'));

            }

            else {

               $selector = $this->selector;

            }

            $js =<<<EOP

$('#{$selector}').ColorPicker({

   {$js_effects}

	onChange: function (hsb, hex, rgb) {

		$('#{$selector}').css('backgroundColor', '#' + hex);

	}

});

EOP;

            $cs->registerScript(get_class($this).'_'.$id, $js);

            echo '';

            break;

      }

   }

}



when a run unit test, display this error:

CException: EColorPicker must specify "model" and "attribute" or "name" property values.

help me please

Your widget must be derrivied from CInputWidget and then you can do that:




$this->widget('MyWidgetClass',array(

'model'=>$myModel,

'attribute'=>'myModelAttributeName',

));