PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/yii/framework/web/form/CFormInputElement.php

https://github.com/ashie1287/headfirst
PHP | 235 lines | 104 code | 15 blank | 116 comment | 6 complexity | fa14007a2cfccca4849436bf84a5b136 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * CFormInputElement class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2010 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CFormInputElement represents form input element.
  12. *
  13. * CFormInputElement can represent the following types of form input based on {@link type} property:
  14. * <ul>
  15. * <li>text: a normal text input generated using {@link CHtml::activeTextField}</li>
  16. * <li>hidden: a hidden input generated using {@link CHtml::activeHiddenField}</li>
  17. * <li>password: a password input generated using {@link CHtml::activePasswordField}</li>
  18. * <li>textarea: a text area generated using {@link CHtml::activeTextArea}</li>
  19. * <li>file: a file input generated using {@link CHtml::activeFileField}</li>
  20. * <li>radio: a radio button generated using {@link CHtml::activeRadioButton}</li>
  21. * <li>checkbox: a check box generated using {@link CHtml::activeCheckBox}</li>
  22. * <li>listbox: a list box generated using {@link CHtml::activeListBox}</li>
  23. * <li>dropdownlist: a drop-down list generated using {@link CHtml::activeDropDownList}</li>
  24. * <li>checkboxlist: a list of check boxes generated using {@link CHtml::activeCheckBoxList}</li>
  25. * <li>radiolist: a list of radio buttons generated using {@link CHtml::activeRadioButtonList}</li>
  26. * </ul>
  27. * The {@link type} property can also be a class name or a path alias to the class. In this case,
  28. * the input is generated using a widget of the specified class. Note, the widget must
  29. * have a property called "model" which expects a model object, and a property called "attribute"
  30. * which expects the name of a model attribute.
  31. *
  32. * Because CFormElement is an ancestor class of CFormInputElement, a value assigned to a non-existing property will be
  33. * stored in {@link attributes} which will be passed as HTML attribute values to the {@link CHtml} method
  34. * generating the input or initial values of the widget properties.
  35. *
  36. * @author Qiang Xue <qiang.xue@gmail.com>
  37. * @version $Id: CFormInputElement.php 2411 2010-09-01 20:21:57Z qiang.xue $
  38. * @package system.web.form
  39. * @since 1.1
  40. */
  41. class CFormInputElement extends CFormElement
  42. {
  43. /**
  44. * @var array Core input types (alias=>CHtml method name)
  45. */
  46. public static $coreTypes=array(
  47. 'text'=>'activeTextField',
  48. 'hidden'=>'activeHiddenField',
  49. 'password'=>'activePasswordField',
  50. 'textarea'=>'activeTextArea',
  51. 'file'=>'activeFileField',
  52. 'radio'=>'activeRadioButton',
  53. 'checkbox'=>'activeCheckBox',
  54. 'listbox'=>'activeListBox',
  55. 'dropdownlist'=>'activeDropDownList',
  56. 'checkboxlist'=>'activeCheckBoxList',
  57. 'radiolist'=>'activeRadioButtonList',
  58. );
  59. /**
  60. * @var string the type of this input. This can be a widget class name, a path alias of a widget class name,
  61. * or a input type alias (text, hidden, password, textarea, file, radio, checkbox, listbox, dropdownlist, checkboxlist, or radiolist).
  62. * If a widget class, it must extend from {@link CInputWidget} or (@link CJuiInputWidget).
  63. */
  64. public $type;
  65. /**
  66. * @var string name of this input
  67. */
  68. public $name;
  69. /**
  70. * @var string hint text of this input
  71. */
  72. public $hint;
  73. /**
  74. * @var array the options for this input when it is a list box, drop-down list, check box list, or radio button list.
  75. * Please see {@link CHtml::listData} for details of generating this property value.
  76. */
  77. public $items=array();
  78. /**
  79. * @var array the options used when rendering the error part. This property will be passed
  80. * to the {@link CActiveForm::error} method call.
  81. * @see CActiveForm::error
  82. * @since 1.1.1
  83. */
  84. public $errorOptions=array();
  85. /**
  86. * @var string the layout used to render label, input, hint and error. They correspond to the placeholders
  87. * "{label}", "{input}", "{hint}" and "{error}".
  88. */
  89. public $layout="{label}\n{input}\n{hint}\n{error}";
  90. private $_label;
  91. private $_required;
  92. /**
  93. * Gets the value indicating whether this input is required.
  94. * If this property is not set explicitly, it will be determined by calling
  95. * {@link CModel::isAttributeRequired} for the associated model and attribute of this input.
  96. * @return boolean whether this input is required.
  97. */
  98. public function getRequired()
  99. {
  100. if($this->_required!==null)
  101. return $this->_required;
  102. else
  103. return $this->getParent()->getModel()->isAttributeRequired($this->name);
  104. }
  105. /**
  106. * @param boolean whether this input is required.
  107. */
  108. public function setRequired($value)
  109. {
  110. $this->_required=$value;
  111. }
  112. /**
  113. * @return string the label for this input. If the label is not manually set,
  114. * this method will call {@link CModel::getAttributeLabel} to determine the label.
  115. */
  116. public function getLabel()
  117. {
  118. if($this->_label!==null)
  119. return $this->_label;
  120. else
  121. return $this->getParent()->getModel()->getAttributeLabel($this->name);
  122. }
  123. /**
  124. * @param string the label for this input
  125. */
  126. public function setLabel($value)
  127. {
  128. $this->_label=$value;
  129. }
  130. /**
  131. * Renders everything for this input.
  132. * The default implementation simply returns the result of {@link renderLabel}, {@link renderInput},
  133. * {@link renderHint}. When {@link CForm::showErrorSummary} is false, {@link renderError} is also called
  134. * to show error messages after individual input fields.
  135. * @return string the complete rendering result for this input, including label, input field, hint, and error.
  136. */
  137. public function render()
  138. {
  139. if($this->type==='hidden')
  140. return $this->renderInput();
  141. $output=array(
  142. '{label}'=>$this->renderLabel(),
  143. '{input}'=>$this->renderInput(),
  144. '{hint}'=>$this->renderHint(),
  145. '{error}'=>$this->getParent()->showErrorSummary ? '' : $this->renderError(),
  146. );
  147. return strtr($this->layout,$output);
  148. }
  149. /**
  150. * Renders the label for this input.
  151. * The default implementation returns the result of {@link CHtml activeLabelEx}.
  152. * @return string the rendering result
  153. */
  154. public function renderLabel()
  155. {
  156. $options = array(
  157. 'label'=>$this->getLabel(),
  158. 'required'=>$this->getRequired()
  159. );
  160. if(!empty($this->attributes['id']))
  161. {
  162. $options['for'] = $this->attributes['id'];
  163. }
  164. return CHtml::activeLabel($this->getParent()->getModel(), $this->name, $options);
  165. }
  166. /**
  167. * Renders the input field.
  168. * The default implementation returns the result of the appropriate CHtml method or the widget.
  169. * @return string the rendering result
  170. */
  171. public function renderInput()
  172. {
  173. if(isset(self::$coreTypes[$this->type]))
  174. {
  175. $method=self::$coreTypes[$this->type];
  176. if(strpos($method,'List')!==false)
  177. return CHtml::$method($this->getParent()->getModel(), $this->name, $this->items, $this->attributes);
  178. else
  179. return CHtml::$method($this->getParent()->getModel(), $this->name, $this->attributes);
  180. }
  181. else
  182. {
  183. $attributes=$this->attributes;
  184. $attributes['model']=$this->getParent()->getModel();
  185. $attributes['attribute']=$this->name;
  186. ob_start();
  187. $this->getParent()->getOwner()->widget($this->type, $attributes);
  188. return ob_get_clean();
  189. }
  190. }
  191. /**
  192. * Renders the error display of this input.
  193. * The default implementation returns the result of {@link CHtml::error}
  194. * @return string the rendering result
  195. */
  196. public function renderError()
  197. {
  198. $parent=$this->getParent();
  199. return $parent->getActiveFormWidget()->error($parent->getModel(), $this->name, $this->errorOptions);
  200. }
  201. /**
  202. * Renders the hint text for this input.
  203. * The default implementation returns the {@link hint} property enclosed in a paragraph HTML tag.
  204. * @return string the rendering result.
  205. */
  206. public function renderHint()
  207. {
  208. return $this->hint===null ? '' : '<div class="hint">'.$this->hint.'</div>';
  209. }
  210. /**
  211. * Evaluates the visibility of this element.
  212. * This method will check if the attribute associated with this input is safe for
  213. * the current model scenario.
  214. * @return boolean whether this element is visible.
  215. */
  216. protected function evaluateVisible()
  217. {
  218. return $this->getParent()->getModel()->isAttributeSafe($this->name);
  219. }
  220. }