LoginSignup
9
8

More than 5 years have passed since last update.

Cakephp3のFormHelperでradioをlabelの外側に出す。

Last updated at Posted at 2016-08-01

ずっと、FormHelper$_defaultConfigtemplateslabelradioWrapperを変更して思った通りに出力されないなって悩んでいたんだけど、nestingLabelradioWrapperを直せばよかった。100マン年悩んでいたのが浄化した。

FormHelperでのデフォルト定義は、以下のようになっています。

vendor/cakephp/cakephp/src/View/Helper/FormHelper.php
// snip
protected $_defaultConfig = [
    /// snip
    'templates' => [
        'label' => '<label{{attrs}}>{{text}}</label>',
        'nestingLabel' => '{{hidden}}<label{{attrs}}>{{input}}{{text}}</label>',
    // snip

これを、以下のようにテンプレートで指定し直す。

hoge.ctp
<?php
$this->Form->templates([
    'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
    'radioWrapper' => '<div class="wrapper-radio">{{label}}</div>',
]);
?>
<?= $this->Form->radio('job', ['1'=>'アルバイト','2'=>'正社員']);?>

出力

hoge.html
<input type="hidden" name="job" value="">
<div class="wrapper-radio">
    <input type="radio" name="job" value="1" id="job-1">
    <label for="job-1"> アルバイト </label>
    </div>
<div class="wrapper-radio">
    <input type="radio" name="job" value="2" id="job-2">
    <label for="job-2">正社員</label>
</div>

参考
* form helpers - Custom templates using 'radioContainer' in Cakephp 3 - Stack Overflow
* 参考:cakephp/src/View/Helper/FormHelper.php

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8