cakephp3 bootstrap3 afterinput に 対応させるマル秘テクニック

こんにちわ。
cakephp3 で afterinput 対応したいけど、どうすりゃいいん？
とお悩みの奥様必見です。

![alt](http://www.sanisoft.com/blog/wp-content/uploads/2014/08/posts-title.png)

こんな感じのですね。

/config/app_form.php を作る

```app_form.php
<?php
$config = [
    'button' => '<button{{attrs}} class="btn btn-default">{{text}}</button>',
    'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
    'before_addoninput' => '<div class="input-group"><span class="input-group-addon">{{before_addon}}</span><input type="{{type}}" name="{{name}}"{{attrs}}></div>',
    'after_addoninput' => '<div class="input-group"><input type="{{type}}" name="{{name}}"{{attrs}}><span class="input-group-addon">{{after_addon}}</span></div>',
    'dateWidget' => '<ul class="list-inline"><li class="year">{{year}}<label>年</label></li><li class="month"> {{month}} <label>月</label> </li><li class="day"> {{day}} <label>日</label> </li></ul>',
    'error' => '<div class="error-message">{{content}}</div>',
    'errorList' => '<ul>{{content}}</ul>',
    'errorItem' => '<li>{{text}}</li>',

    'file' => '<label for="{{name}}">+ 選択して下さい<input type="file" name="{{name}}"{{attrs}}></label>',

    'fieldset' => '<fieldset>{{content}}</fieldset>',
    'formstart' => '<form{{attrs}}>',
    'formend' => '</form>',
    'formGroup' => '{{label}}{{input}}',
    'hiddenblock' => '<div style="display:none;">{{content}}</div>',
    'input' => '<input class="form-control" type="{{type}}" name="{{name}}"{{attrs}}>',
    'inputSubmit' => '<input type="{{type}}"{{attrs}}>',
    'inputContainer' => '<div class="form-group {{type}}{{required}}">{{content}}</div>',
    'inputContainerError' => '<div class="form-group {{type}}{{required}} has-error">{{content}}{{error}}</div>',
    'label' => '{{text}}',
    'legend' => '<legend>{{text}}</legend>',
    'option' => '<option value="{{value}}"{{attrs}}>{{text}}</option>',
    'optgroup' => '<optgroup label="{{label}}"{{attrs}}>{{content}}</optgroup>',
    'select' => '<select class="form-control" name="{{name}}"{{attrs}}>{{content}}</select>',
    'selectMultiple' => '<select class="form-control" name="{{name}}[]" multiple="multiple"{{attrs}}>{{content}}</select>',
    'radio' => '<input type="radio" name="{{name}}" value="{{value}}"{{attrs}}>',
    'radioWrapper' => '<div class="radio"><label>{{input}} {{label}}</label></div>',
    'textarea' => '<textarea class="form-control" name="{{name}}"{{attrs}}>{{value}}</textarea>',
    'submitContainer' => '<div class="submit">{{content}}</div>',
];

?>
```

Formヘルパーの読み込みを変更

```AppController.php
    public function initialize()
    {
        parent::initialize();

        $this->viewBuilder()->helpers([
            'Form' => ['templates' => 'app_form']
        ]);
    }
```

BasicWidget.php を変更
cakephp/src/View/Widget を app/src/View/Widget にコピーして
バージョンによって違うみたいなので。注意。。。

```BasicWidget.php
    public function render(array $data, ContextInterface $context) {
        $data += [
            'name' => '',
            'val' => null,
            'type' => null,
            'escape' => true,
            'templateVars' => []
        ];

        $data['value'] = $data['val'];
        $before_addon = '';
        $after_addon = '';

        $template = 'input';

        if(isset($data['before_addon'])) {
            $before_addon = $data['before_addon'];
            $template = 'before_addoninput';
        }

        if(isset($data['after_addon'])) {
            $after_addon = $data['after_addon'];
            $template = 'after_addoninput';
        }

        unset($data['val'], $data['before_addon']);

        return $this->_templates->format($template, [
            'name' => $data['name'],
            'before_addon' => $before_addon,
            'after_addon' => $after_addon,

            'type' => $data['type'],
            'templateVars' => $data['templateVars'],
            'attrs' => $this->_templates->formatAttributes(
                $data,
                ['name', 'type']
            ),
        ]);
    }
```

であとは呼び出すだけで・・・

```php

    <?php
    echo $this->Form->input('title', ['before_addon'=>'@', "class"=>"form-control"]);
    ?>

    <?php
    echo $this->Form->input('hoge', ['after_addon'=>'cm', "class"=>"form-control"]);
    ?>
```

なんとぱっくり！
うまく表示できたではありませんか。


:relaxed: