BcFormHelper::checkbox PHP Method

checkbox() public method

MODIFIED 2008/10/24 egashira hiddenタグを出力しないオプションを追加 ### 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
$fieldName string Name of a field, like this "Modelname.fieldname"
$options array Array of HTML attributes.
return string An HTML text input element.
    public function checkbox($fieldName, $options = array())
    {
        // CUSTOMIZE ADD 2011/05/07 ryuring
        // >>> hiddenをデフォルトオプションに追加
        $options = array_merge(array('hidden' => true), $options);
        $hidden = $options['hidden'];
        unset($options['hidden']);
        // <<<
        $valueOptions = array();
        if (isset($options['default'])) {
            $valueOptions['default'] = $options['default'];
            unset($options['default']);
        }
        $options += array('value' => 1, 'required' => false);
        $options = $this->_initInputField($fieldName, $options) + array('hiddenField' => true);
        $value = current($this->value($valueOptions));
        $output = '';
        if (!isset($options['checked']) && !empty($value) && $value == $options['value'] || !empty($options['checked'])) {
            $options['checked'] = 'checked';
        }
        // CUSTOMIZE MODIFY 2011/05/07 ryuring
        // >>> hiddenオプションがある場合のみ、hiddenタグを出力
        // 2014/03/23 ryuring CakePHP側が実装していたが互換性の為に残す
        //if ($options['hiddenField']) {
        // ---
        if ($hidden !== false && $options['hiddenField'] !== false) {
            // <<<
            $hiddenOptions = array('id' => $options['id'] . '_', 'name' => $options['name'], 'value' => $options['hiddenField'] !== true ? $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);
        }
        unset($options['hiddenField']);
        // CUSTOMIZE MODIFY 2011/05/07 ryuring
        // label を追加
        // CUSTOMIZE MODIRY 2014/10/27 ryuring
        // チェックボックスをラベルで囲う仕様に変更
        // >>>
        //return $output . $this->Html->useTag('checkbox', $options['name'], array_diff_key($options, array('name' => null)));
        // ---
        if (!empty($options['label'])) {
            return $output . parent::label($fieldName, $this->Html->useTag('checkbox', $options['name'], array_diff_key($options, array('name' => null))) . $options['label']);
        } else {
            return $output . $this->Html->useTag('checkbox', $options['name'], array_diff_key($options, array('name' => null)));
        }
        // <<<
    }

Usage Example

Example #1
0
 /**
  * チェックボックスを表示する
  * 
  * @param string $fieldName フィールド文字列
  * @param array $attributes html属性
  * @return string htmlタグ
  * @access public
  */
 public function checkbox($fieldName, $attributes = array())
 {
     if ($this->freezed) {
         $label = '';
         if (isset($attributes['label'])) {
             $label = $attributes['label'];
         }
         $options = array(0 => '', 1 => $label);
         return $this->freezeControll($fieldName, $options, $attributes);
     } else {
         return parent::checkbox($fieldName, $attributes);
     }
 }
All Usage Examples Of BcFormHelper::checkbox