PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/web/form/CFormInputElement.php

https://bitbucket.org/dinhtrung/yiicorecms/
PHP | 251 lines | 106 code | 15 blank | 130 comment | 6 complexity | 19b80a0c56fb2f30513b5ee91eefb8e8 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, CC0-1.0, BSD-2-Clause, GPL-2.0, LGPL-2.1, LGPL-3.0
  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-2011 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 3126 2011-03-26 12:21:13Z 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 as its $htmlOptions parameter.
  81. * @see CActiveForm::error
  82. * @since 1.1.1
  83. */
  84. public $errorOptions=array();
  85. /**
  86. * @var boolean whether to allow AJAX-based validation for this input. Note that in order to use
  87. * AJAX-based validation, {@link CForm::activeForm} must be configured with 'enableAjaxValidation'=>true.
  88. * This property allows turning on or off AJAX-based validation for individual input fields.
  89. * Defaults to true.
  90. * @since 1.1.7
  91. */
  92. public $enableAjaxValidation=true;
  93. /**
  94. * @var boolean whether to allow client-side validation for this input. Note that in order to use
  95. * client-side validation, {@link CForm::activeForm} must be configured with 'enableClientValidation'=>true.
  96. * This property allows turning on or off client-side validation for individual input fields.
  97. * Defaults to true.
  98. * @since 1.1.7
  99. */
  100. public $enableClientValidation=true;
  101. /**
  102. * @var string the layout used to render label, input, hint and error. They correspond to the placeholders
  103. * "{label}", "{input}", "{hint}" and "{error}".
  104. */
  105. public $layout="{label}\n{input}\n{hint}\n{error}";
  106. private $_label;
  107. private $_required;
  108. /**
  109. * Gets the value indicating whether this input is required.
  110. * If this property is not set explicitly, it will be determined by calling
  111. * {@link CModel::isAttributeRequired} for the associated model and attribute of this input.
  112. * @return boolean whether this input is required.
  113. */
  114. public function getRequired()
  115. {
  116. if($this->_required!==null)
  117. return $this->_required;
  118. else
  119. return $this->getParent()->getModel()->isAttributeRequired($this->name);
  120. }
  121. /**
  122. * @param boolean $value whether this input is required.
  123. */
  124. public function setRequired($value)
  125. {
  126. $this->_required=$value;
  127. }
  128. /**
  129. * @return string the label for this input. If the label is not manually set,
  130. * this method will call {@link CModel::getAttributeLabel} to determine the label.
  131. */
  132. public function getLabel()
  133. {
  134. if($this->_label!==null)
  135. return $this->_label;
  136. else
  137. return $this->getParent()->getModel()->getAttributeLabel($this->name);
  138. }
  139. /**
  140. * @param string $value the label for this input
  141. */
  142. public function setLabel($value)
  143. {
  144. $this->_label=$value;
  145. }
  146. /**
  147. * Renders everything for this input.
  148. * The default implementation simply returns the result of {@link renderLabel}, {@link renderInput},
  149. * {@link renderHint}. When {@link CForm::showErrorSummary} is false, {@link renderError} is also called
  150. * to show error messages after individual input fields.
  151. * @return string the complete rendering result for this input, including label, input field, hint, and error.
  152. */
  153. public function render()
  154. {
  155. if($this->type==='hidden')
  156. return $this->renderInput();
  157. $output=array(
  158. '{label}'=>$this->renderLabel(),
  159. '{input}'=>$this->renderInput(),
  160. '{hint}'=>$this->renderHint(),
  161. '{error}'=>$this->getParent()->showErrorSummary ? '' : $this->renderError(),
  162. );
  163. return strtr($this->layout,$output);
  164. }
  165. /**
  166. * Renders the label for this input.
  167. * The default implementation returns the result of {@link CHtml activeLabelEx}.
  168. * @return string the rendering result
  169. */
  170. public function renderLabel()
  171. {
  172. $options = array(
  173. 'label'=>$this->getLabel(),
  174. 'required'=>$this->getRequired()
  175. );
  176. if(!empty($this->attributes['id']))
  177. {
  178. $options['for'] = $this->attributes['id'];
  179. }
  180. return CHtml::activeLabel($this->getParent()->getModel(), $this->name, $options);
  181. }
  182. /**
  183. * Renders the input field.
  184. * The default implementation returns the result of the appropriate CHtml method or the widget.
  185. * @return string the rendering result
  186. */
  187. public function renderInput()
  188. {
  189. if(isset(self::$coreTypes[$this->type]))
  190. {
  191. $method=self::$coreTypes[$this->type];
  192. if(strpos($method,'List')!==false)
  193. return CHtml::$method($this->getParent()->getModel(), $this->name, $this->items, $this->attributes);
  194. else
  195. return CHtml::$method($this->getParent()->getModel(), $this->name, $this->attributes);
  196. }
  197. else
  198. {
  199. $attributes=$this->attributes;
  200. $attributes['model']=$this->getParent()->getModel();
  201. $attributes['attribute']=$this->name;
  202. ob_start();
  203. $this->getParent()->getOwner()->widget($this->type, $attributes);
  204. return ob_get_clean();
  205. }
  206. }
  207. /**
  208. * Renders the error display of this input.
  209. * The default implementation returns the result of {@link CHtml::error}
  210. * @return string the rendering result
  211. */
  212. public function renderError()
  213. {
  214. $parent=$this->getParent();
  215. return $parent->getActiveFormWidget()->error($parent->getModel(), $this->name, $this->errorOptions, $this->enableAjaxValidation, $this->enableClientValidation);
  216. }
  217. /**
  218. * Renders the hint text for this input.
  219. * The default implementation returns the {@link hint} property enclosed in a paragraph HTML tag.
  220. * @return string the rendering result.
  221. */
  222. public function renderHint()
  223. {
  224. return $this->hint===null ? '' : '<div class="hint">'.$this->hint.'</div>';
  225. }
  226. /**
  227. * Evaluates the visibility of this element.
  228. * This method will check if the attribute associated with this input is safe for
  229. * the current model scenario.
  230. * @return boolean whether this element is visible.
  231. */
  232. protected function evaluateVisible()
  233. {
  234. return $this->getParent()->getModel()->isAttributeSafe($this->name);
  235. }
  236. }