Cake\View\Helper\FormHelper::checkbox PHP Method

checkbox() public method

### Options: - value - the value of the checkbox - checked - boolean indicate that this checkbox is checked. - hiddenField - boolean to indicate if you want the results of checkbox() to include a hidden input with a value of ''. - disabled - create a disabled input. - default - Set the default value for the checkbox. This allows you to start checkboxes as checked, without having to check the POST data. A matching POST data value, will overwrite the default value.
public checkbox ( string $fieldName, array $options = [] ) : string | array
$fieldName string Name of a field, like this "modelname.fieldname"
$options array Array of HTML attributes.
return string | array An HTML text input element.
    public function checkbox($fieldName, array $options = [])
    {
        $options += ['hiddenField' => true, 'value' => 1];
        // Work around value=>val translations.
        $value = $options['value'];
        unset($options['value']);
        $options = $this->_initInputField($fieldName, $options);
        $options['value'] = $value;
        $output = '';
        if ($options['hiddenField']) {
            $hiddenOptions = ['name' => $options['name'], 'value' => $options['hiddenField'] !== true && $options['hiddenField'] !== '_split' ? $options['hiddenField'] : '0', 'form' => isset($options['form']) ? $options['form'] : null, 'secure' => false];
            if (isset($options['disabled']) && $options['disabled']) {
                $hiddenOptions['disabled'] = 'disabled';
            }
            $output = $this->hidden($fieldName, $hiddenOptions);
        }
        if ($options['hiddenField'] === '_split') {
            unset($options['hiddenField'], $options['type']);
            return ['hidden' => $output, 'input' => $this->widget('checkbox', $options)];
        }
        unset($options['hiddenField'], $options['type']);
        return $output . $this->widget('checkbox', $options);
    }

Usage Example

Beispiel #1
0
 /**
  * Creates a checkbox input element
  * @param string $fieldName Field name, should be "Modelname.fieldname"
  * @param array $options HTML attributes and options
  * @return string
  */
 public function checkbox($fieldName, array $options = [])
 {
     if (!isset($options['hiddenField']) || !empty($options['hiddenField'])) {
         $options['hiddenField'] = true;
     }
     return parent::checkbox($fieldName, $options);
 }
All Usage Examples Of Cake\View\Helper\FormHelper::checkbox