cakephp3 bootstrap3 afterinput 에 대응시키는 말비 기법

14471 단어 Bootstrap3cakephp3
안녕하세요.
cakephp3 에서 afterinput 대응하고 싶지만, 어떻게 하면 좋을까?
라고 고민의 부인 필견입니다.

alt

이런 느낌이군요.

/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>',
];

?>

양식 도우미 로드 변경

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

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

무려 푹 빠져!
잘 볼 수 없었나요?

좋은 웹페이지 즐겨찾기