CakePHPまとめ@Wiki

ラジオボタンのNULLチェック

最終更新:

nezox

- view
管理者のみ編集可

Html Helperのradioを生成する時、ラジオボタンの前に同じnameのhiddenを生成する事で、チェックが一つも無かった場合でもValidation出来るようにします。

/cake/libs/view/helpers/html.phpを改造します。

radio関数に下記のように追記します。

---この部分を追記します-------
// Auto Render Hidenn For Null Check / Nezox:2006.08.12
$out[] = sprintf($this->tags['hidden'], $this->model, $this->field, $this->parseHtmlOptions(array('value'=>'')), null);
----------------------------------------


【/cake/libs/view/helpers/html.php】

/**
* Creates a set of radio widgets.
*
* @param string $fieldName Name of a field, like this "Modelname/fieldname"
* @param array  $options      Radio button options array
* @param array  $inbetween    String that separates the radio buttons.
* @param array  $htmlAttributes Array of HTML attributes.
* @param boolean $return  Wheter this method should return a value or output it. This overrides AUTO_OUTPUT.
* @return mixed  Either string or boolean value, depends on AUTO_OUTPUT and $return.
*/
function radio($fieldName, $options, $inbetween = null, $htmlAttributes = array(), $return = false) {

$this->setFormTag($fieldName);
$value = isset($htmlAttributes['value']) ? $htmlAttributes['value'] : $this->tagValue($fieldName);
$out = array();

// Auto Render Hidenn For Null Check / Nezox:2006.08.12
$out[] = sprintf($this->tags['hidden'], $this->model, $this->field, $this->parseHtmlOptions(array('value'=>'')), null);

foreach($options as $optValue => $optTitle) {
$optionsHere = array('value' => $optValue);
$optValue == $value ? $optionsHere['checked'] = 'checked' : null;
$parsedOptions = $this->parseHtmlOptions(array_merge($htmlAttributes, $optionsHere), null, '', ' ');
$individualTagName = "{$this->field}_{$optValue}";
$out[] = sprintf($this->tags['radio'], $this->model, $this->field, $individualTagName, $parsedOptions, $optTitle);
}

$out = join($inbetween, $out);
return $this->output($out ? $out : null, $return);
}

目安箱バナー