CInputWidget.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * CInputWidget class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CInputWidget is the base class for widgets that collect user inputs.
  12. *
  13. * CInputWidget declares properties common among input widgets. An input widget
  14. * can be associated with a data model and an attribute, or a name and a value.
  15. * If the former, the name and the value will be generated automatically.
  16. * Child classes may use {@link resolveNameID} and {@link hasModel}.
  17. *
  18. * @author Qiang Xue <qiang.xue@gmail.com>
  19. * @package system.web.widgets
  20. * @since 1.0
  21. */
  22. abstract class CInputWidget extends CWidget
  23. {
  24. /**
  25. * @var CModel the data model associated with this widget.
  26. */
  27. public $model;
  28. /**
  29. * @var string the attribute associated with this widget.
  30. * The name can contain square brackets (e.g. 'name[1]') which is used to collect tabular data input.
  31. */
  32. public $attribute;
  33. /**
  34. * @var string the input name. This must be set if {@link model} is not set.
  35. */
  36. public $name;
  37. /**
  38. * @var string the input value
  39. */
  40. public $value;
  41. /**
  42. * @var array additional HTML options to be rendered in the input tag
  43. */
  44. public $htmlOptions=array();
  45. /**
  46. * @return array the name and the ID of the input.
  47. * @throws CException in case input name and ID cannot be resolved.
  48. */
  49. protected function resolveNameID()
  50. {
  51. if($this->name!==null)
  52. $name=$this->name;
  53. elseif(isset($this->htmlOptions['name']))
  54. $name=$this->htmlOptions['name'];
  55. elseif($this->hasModel())
  56. $name=CHtml::activeName($this->model,$this->attribute);
  57. else
  58. throw new CException(Yii::t('yii','{class} must specify "model" and "attribute" or "name" property values.',array('{class}'=>get_class($this))));
  59. if(($id=$this->getId(false))===null)
  60. {
  61. if(isset($this->htmlOptions['id']))
  62. $id=$this->htmlOptions['id'];
  63. else
  64. $id=CHtml::getIdByName($name);
  65. }
  66. return array($name,$id);
  67. }
  68. /**
  69. * @return boolean whether this widget is associated with a data model.
  70. */
  71. protected function hasModel()
  72. {
  73. return $this->model instanceof CModel && $this->attribute!==null;
  74. }
  75. }