TYPO3 API  SVNRelease
CheckboxViewHelper.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*                                                                        *
00004  * This script belongs to the FLOW3 package "Fluid".                      *
00005  *                                                                        *
00006  * It is free software; you can redistribute it and/or modify it under    *
00007  * the terms of the GNU Lesser General Public License as published by the *
00008  * Free Software Foundation, either version 3 of the License, or (at your *
00009  * option) any later version.                                             *
00010  *                                                                        *
00011  * This script is distributed in the hope that it will be useful, but     *
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
00013  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
00014  * General Public License for more details.                               *
00015  *                                                                        *
00016  * You should have received a copy of the GNU Lesser General Public       *
00017  * License along with the script.                                         *
00018  * If not, see http://www.gnu.org/licenses/lgpl.html                      *
00019  *                                                                        *
00020  * The TYPO3 project - inspiring people to share!                         *
00021  *                                                                        */
00022 
00023 /**
00024  * View Helper which creates a simple checkbox (<input type="checkbox">).
00025  *
00026  * = Examples =
00027  *
00028  * <code title="Example">
00029  * <f:form.checkbox name="myCheckBox" value="someValue" />
00030  * </code>
00031  * <output>
00032  * <input type="checkbox" name="myCheckBox" value="someValue" />
00033  * </output>
00034  *
00035  * <code title="Preselect">
00036  * <f:form.checkbox name="myCheckBox" value="someValue" checked="{object.value} == 5" />
00037  * </code>
00038  * <output>
00039  * <input type="checkbox" name="myCheckBox" value="someValue" checked="checked" />
00040  * (depending on $object)
00041  * </output>
00042  *
00043  * <code title="Bind to object property">
00044  * <f:form.checkbox property="interests" value="TYPO3" />
00045  * </code>
00046  * <output>
00047  * <input type="checkbox" name="user[interests][]" value="TYPO3" checked="checked" />
00048  * (depending on property "interests")
00049  * </output>
00050  *
00051  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
00052  * @api
00053  */
00054 class Tx_Fluid_ViewHelpers_Form_CheckboxViewHelper extends Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper {
00055 
00056     /**
00057      * @var string
00058      */
00059     protected $tagName = 'input';
00060 
00061     /**
00062      * Initialize the arguments.
00063      *
00064      * @return void
00065      * @author Bastian Waidelich <bastian@typo3.org>
00066      * @api
00067      */
00068     public function initializeArguments() {
00069         parent::initializeArguments();
00070         $this->registerTagAttribute('disabled', 'string', 'Specifies that the input element should be disabled when the page loads');
00071         $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this view helper', FALSE, 'f3-form-error');
00072         $this->overrideArgument('value', 'string', 'Value of input tag. Required for checkboxes', TRUE);
00073         $this->registerUniversalTagAttributes();
00074     }
00075 
00076     /**
00077      * Renders the checkbox.
00078      *
00079      * @param boolean $checked Specifies that the input element should be preselected
00080      *
00081      * @return string
00082      * @author Bastian Waidelich <bastian@typo3.org>
00083      * @api
00084      */
00085     public function render($checked = NULL) {
00086         $this->tag->addAttribute('type', 'checkbox');
00087 
00088         $nameAttribute = $this->getName();
00089         $valueAttribute = $this->getValue();
00090         if ($checked === NULL && $this->isObjectAccessorMode()) {
00091             $propertyValue = $this->getPropertyValue();
00092             if (is_bool($propertyValue)) {
00093                 $checked = $propertyValue === (boolean)$valueAttribute;
00094             } elseif (is_array($propertyValue)) {
00095                 $checked = in_array($valueAttribute, $propertyValue);
00096                 $nameAttribute .= '[]';
00097             } else {
00098                 throw new Tx_Fluid_Core_ViewHelper_Exception('Checkbox viewhelpers can only be bound to properties of type boolean or array. Property "' . $this->arguments['property'] . '" is of type "' . (is_object($propertyValue) ? get_class($propertyValue) : gettype($propertyValue)) . '".' , 1248261038);
00099             }
00100         }
00101 
00102         $this->registerFieldNameForFormTokenGeneration($nameAttribute);
00103         $this->tag->addAttribute('name', $nameAttribute);
00104         $this->tag->addAttribute('value', $valueAttribute);
00105         if ($checked) {
00106             $this->tag->addAttribute('checked', 'checked');
00107         }
00108 
00109         $this->setErrorClassAttribute();
00110 
00111         $hiddenField = $this->renderHiddenFieldForEmptyValue();
00112         return $hiddenField . $this->tag->render();
00113     }
00114 }
00115 
00116 ?>