5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright © 2005-2014 PradoSoft
8 * @license http://www.pradosoft.com/license/
9 * @package System.Web.UI.WebControls
15 * TCheckBox displays a check box on the page.
16 * You can specify the caption to display beside the check box by setting
17 * the {@link setText Text} property. The caption can appear either on the right
18 * or left of the check box, which is determined by the {@link setTextAlign TextAlign}
21 * To determine whether the TCheckBox component is checked, test the {@link getChecked Checked}
22 * property. The {@link onCheckedChanged OnCheckedChanged} event is raised when
23 * the {@link getChecked Checked} state of the TCheckBox component changes
24 * between posts to the server. You can provide an event handler for
25 * the {@link onCheckedChanged OnCheckedChanged} event to to programmatically
26 * control the actions performed when the state of the TCheckBox component changes
27 * between posts to the server.
29 * If {@link setAutoPostBack AutoPostBack} is set true, changing the check box state
30 * will cause postback action. And if {@link setCausesValidation CausesValidation}
31 * is true, validation will also be processed, which can be further restricted within
32 * a {@link setValidationGroup ValidationGroup}.
34 * Note, {@link setText Text} is rendered as is. Make sure it does not contain unwanted characters
35 * that may bring security vulnerabilities.
37 * @author Qiang Xue <qiang.xue@gmail.com>
38 * @package System.Web.UI.WebControls
41 class TCheckBox extends TWebControl implements IPostBackDataHandler, IValidatable, IDataRenderer, ISurroundable
43 private $_dataChanged=false;
44 private $_isValid=true;
47 * @return string tag name of the button
49 protected function getTagName()
55 * Loads user input data.
56 * This method is primarly used by framework developers.
57 * @param string the key that can be used to retrieve data from the input data collection
58 * @param array the input data collection
59 * @return boolean whether the data of the control has been changed
61 public function loadPostData($key,$values)
63 $checked=$this->getChecked();
64 if($newChecked=isset($values[$key]))
65 $this->setValue($values[$key]);
66 $this->setChecked($newChecked);
67 return $this->_dataChanged=($newChecked!==$checked);
71 * Raises postdata changed event.
72 * This method raises {@link onCheckedChanged OnCheckedChanged} event.
73 * This method is primarly used by framework developers.
75 public function raisePostDataChangedEvent()
77 if($this->getAutoPostBack() && $this->getCausesValidation())
78 $this->getPage()->validate($this->getValidationGroup());
79 $this->onCheckedChanged(null);
83 * Raises <b>OnCheckedChanged</b> event when {@link getChecked Checked} changes value during postback.
84 * If you override this method, be sure to call the parent implementation
85 * so that the event delegates can be invoked.
86 * @param TEventParameter event parameter to be passed to the event handlers
88 public function onCheckedChanged($param)
90 $this->raiseEvent('OnCheckedChanged',$this,$param);
94 * Registers the checkbox to receive postback data during postback.
95 * This is necessary because a checkbox if unchecked, when postback,
96 * does not have direct mapping between post data and the checkbox name.
98 * This method overrides the parent implementation and is invoked before render.
99 * @param mixed event parameter
101 public function onPreRender($param)
103 parent::onPreRender($param);
104 if($this->getEnabled(true))
105 $this->getPage()->registerRequiresPostData($this);
109 * Returns a value indicating whether postback has caused the control data change.
110 * This method is required by the IPostBackDataHandler interface.
111 * @return boolean whether postback has caused the control data change. False if the page is not in postback mode.
113 public function getDataChanged()
115 return $this->_dataChanged;
119 * Returns the value of the property that needs validation.
120 * @return mixed the property value to be validated
122 public function getValidationPropertyValue()
124 return $this->getChecked();
128 * Returns true if this control validated successfully.
130 * @return bool wether this control validated successfully.
132 public function getIsValid()
134 return $this->_isValid;
137 * @param bool wether this control is valid.
139 public function setIsValid($value)
141 $this->_isValid=TPropertyValue::ensureBoolean($value);
145 * @return string the text caption of the checkbox
147 public function getText()
149 return $this->getViewState('Text','');
153 * Sets the text caption of the checkbox.
154 * @param string the text caption to be set
156 public function setText($value)
158 $this->setViewState('Text',$value,'');
162 * @return string the value of the checkbox. Defaults to empty.
164 public function getValue()
166 return $this->getViewState('Value','');
170 * @param string the value of the checkbox
172 public function setValue($value)
174 $this->setViewState('Value',TPropertyValue::ensureString($value),'');
178 * @return TTextAlign the alignment (Left or Right) of the text caption, defaults to TTextAlign::Right.
180 public function getTextAlign()
182 return $this->getViewState('TextAlign',TTextAlign::Right);
186 * @param TTextAlign the alignment of the text caption. Valid values include Left and Right.
188 public function setTextAlign($value)
190 $this->setViewState('TextAlign',TPropertyValue::ensureEnum($value,'TTextAlign'),TTextAlign::Right);
194 * @return boolean whether the checkbox is checked
196 public function getChecked()
198 return $this->getViewState('Checked',false);
202 * Sets a value indicating whether the checkbox is to be checked or not.
203 * @param boolean whether the checkbox is to be checked or not.
205 public function setChecked($value)
207 $this->setViewState('Checked',TPropertyValue::ensureBoolean($value),false);
211 * Returns the value indicating whether the checkbox is checked.
212 * This method is required by {@link IDataRenderer}.
213 * It is the same as {@link getChecked()}.
214 * @return boolean whether the checkbox is checked.
218 public function getData()
220 return $this->getChecked();
224 * Sets the value indicating whether the checkbox is to be checked or not.
225 * This method is required by {@link IDataRenderer}.
226 * It is the same as {@link setChecked()}.
227 * @param boolean whether the checkbox is to be checked
231 public function setData($value)
233 $this->setChecked($value);
237 * @return boolean whether clicking on the checkbox will post the page.
239 public function getAutoPostBack()
241 return $this->getViewState('AutoPostBack',false);
245 * Sets a value indicating whether clicking on the checkbox will post the page.
246 * @param boolean whether clicking on the checkbox will post the page.
248 public function setAutoPostBack($value)
250 $this->setViewState('AutoPostBack',TPropertyValue::ensureBoolean($value),false);
254 * @return boolean whether postback event triggered by this checkbox will cause input validation, default is true.
256 public function getCausesValidation()
258 return $this->getViewState('CausesValidation',true);
262 * Sets the value indicating whether postback event trigger by this checkbox will cause input validation.
263 * @param boolean whether postback event trigger by this checkbox will cause input validation.
265 public function setCausesValidation($value)
267 $this->setViewState('CausesValidation',TPropertyValue::ensureBoolean($value),true);
271 * @return string the group of validators which the checkbox causes validation upon postback
273 public function getValidationGroup()
275 return $this->getViewState('ValidationGroup','');
279 * @param string the group of validators which the checkbox causes validation upon postback
281 public function setValidationGroup($value)
283 $this->setViewState('ValidationGroup',$value,'');
287 * @return string the id of the surrounding tag or this clientID if no such tag needed
289 public function getSurroundingTagID()
291 return $this->getSpanNeeded() ? $this->getClientID().'_parent' : $this->getClientID();
295 * Renders the checkbox control.
296 * This method overrides the parent implementation by rendering a checkbox input element
297 * and a span element if needed.
298 * @param THtmlWriter the writer used for the rendering purpose
300 public function render($writer)
302 $this->getPage()->ensureRenderInForm($this);
303 if($this->getHasStyle())
304 $this->getStyle()->addAttributesToRender($writer);
305 if(($tooltip=$this->getToolTip())!=='')
306 $writer->addAttribute('title',$tooltip);
307 if($this->getHasAttributes())
309 $attributes=$this->getAttributes();
310 $value=$attributes->remove('value');
311 // onclick js should only be added to input tag
312 if(($onclick=$attributes->remove('onclick'))===null)
314 if($attributes->getCount())
315 $writer->addAttributes($attributes);
317 $attributes->add('value',$value);
321 if($needspan=$this->getSpanNeeded())
323 $writer->addAttribute('id',$this->getSurroundingTagID());
324 $writer->renderBeginTag('span');
326 $clientID=$this->getClientID();
327 if(($text=$this->getText())!=='')
329 if($this->getTextAlign()===TTextAlign::Left)
331 $this->renderLabel($writer,$clientID,$text);
332 $this->renderInputTag($writer,$clientID,$onclick);
336 $this->renderInputTag($writer,$clientID,$onclick);
337 $this->renderLabel($writer,$clientID,$text);
341 $this->renderInputTag($writer,$clientID,$onclick);
343 $writer->renderEndTag();
347 * @return TMap list of attributes to be rendered for label beside the checkbox
349 public function getLabelAttributes()
351 if($attributes=$this->getViewState('LabelAttributes',null))
355 $attributes=new TAttributeCollection;
356 $this->setViewState('LabelAttributes',$attributes,null);
362 * @return TMap list of attributes to be rendered for the checkbox
364 public function getInputAttributes()
366 if($attributes=$this->getViewState('InputAttributes',null))
370 $attributes=new TAttributeCollection;
371 $this->setViewState('InputAttributes',$attributes,null);
377 * @return string the value attribute to be rendered
379 protected function getValueAttribute()
381 if(($value=$this->getValue())!=='')
385 $attributes=$this->getViewState('InputAttributes',null);
386 if($attributes && $attributes->contains('value'))
387 return $attributes->itemAt('value');
388 else if($this->hasAttribute('value'))
389 return $this->getAttribute('value');
396 * @return boolean whether to render javascript.
398 public function getEnableClientScript()
400 return $this->getViewState('EnableClientScript',true);
404 * @param boolean whether to render javascript.
406 public function setEnableClientScript($value)
408 $this->setViewState('EnableClientScript',TPropertyValue::ensureBoolean($value),true);
412 * Check if we need a span tag to surround this control. The span tag will be created if
413 * the Text property is set for this control.
415 * @return bool wether this control needs a surrounding span tag
417 protected function getSpanNeeded() {
418 return $this->getText()!=='';
422 * Renders a label beside the checkbox.
423 * @param THtmlWriter the writer for the rendering purpose
424 * @param string checkbox id
425 * @param string label text
427 protected function renderLabel($writer,$clientID,$text)
429 $writer->addAttribute('for',$clientID);
430 if($attributes=$this->getViewState('LabelAttributes',null))
431 $writer->addAttributes($attributes);
432 $writer->renderBeginTag('label');
433 $writer->write($text);
434 $writer->renderEndTag();
438 * Renders a checkbox input element.
439 * @param THtmlWriter the writer for the rendering purpose
440 * @param string checkbox id
441 * @param string onclick js
443 protected function renderInputTag($writer,$clientID,$onclick)
446 $writer->addAttribute('id',$clientID);
447 $writer->addAttribute('type','checkbox');
448 if(($value=$this->getValueAttribute())!=='')
449 $writer->addAttribute('value',$value);
451 $writer->addAttribute('onclick',$onclick);
452 if(($uniqueID=$this->getUniqueID())!=='')
453 $writer->addAttribute('name',$uniqueID);
454 if($this->getChecked())
455 $writer->addAttribute('checked','checked');
456 if(!$this->getEnabled(true))
457 $writer->addAttribute('disabled','disabled');
459 $page=$this->getPage();
460 if($this->getEnabled(true)
461 && $this->getEnableClientScript()
462 && $this->getAutoPostBack()
463 && $page->getClientSupportsJavaScript())
465 $this->renderClientControlScript($writer);
468 if(($accesskey=$this->getAccessKey())!=='')
469 $writer->addAttribute('accesskey',$accesskey);
470 if(($tabindex=$this->getTabIndex())>0)
471 $writer->addAttribute('tabindex',"$tabindex");
472 if($attributes=$this->getViewState('InputAttributes',null))
473 $writer->addAttributes($attributes);
474 $writer->renderBeginTag('input');
475 $writer->renderEndTag();
479 * Renders the client-script code.
481 protected function renderClientControlScript($writer)
483 $cs = $this->getPage()->getClientScript();
484 $cs->registerPostBackControl($this->getClientClassName(),$this->getPostBackOptions());
488 * Gets the name of the javascript class responsible for performing postback for this control.
489 * This method overrides the parent implementation.
490 * @return string the javascript class name
492 protected function getClientClassName()
494 return 'Prado.WebUI.TCheckBox';
498 * Gets the post back options for this checkbox.
501 protected function getPostBackOptions()
503 $options['ID'] = $this->getClientID();
504 $options['ValidationGroup'] = $this->getValidationGroup();
505 $options['CausesValidation'] = $this->getCausesValidation();
506 $options['EventTarget'] = $this->getUniqueID();
513 * TTextAlign defines the enumerable type for the possible text alignments
515 * The following enumerable values are defined:
516 * - Left: left aligned
517 * - Right: right aligned
519 * @author Qiang Xue <qiang.xue@gmail.com>
520 * @package System.Web.UI.WebControls
523 class TTextAlign extends TEnumerable