BootstrapUI\View\Helper\FormHelper::input PHP Метод

input() публичный Метод

Adds extra option besides the ones supported by parent class method: - append - Append addon to input. - prepend - Prepend addon to input. - inline - Boolean for generating inline checkbox/radio. - help - Help text of include in the input container.
public input ( string $fieldName, array $options = [] ) : string
$fieldName string This should be "Modelname.fieldname".
$options array Each type of input takes different options.
Результат string Completed form widget.
    public function input($fieldName, array $options = [])
    {
        $options += ['prepend' => null, 'append' => null, 'inline' => null, 'type' => null, 'label' => null, 'error' => null, 'required' => null, 'options' => null, 'help' => null, 'templates' => [], 'templateVars' => []];
        $options = $this->_parseOptions($fieldName, $options);
        $newTemplates = $options['templates'];
        if ($newTemplates) {
            $this->templater()->push();
            $templateMethod = is_string($options['templates']) ? 'load' : 'add';
            $this->templater()->{$templateMethod}($options['templates']);
            $options['templates'] = [];
        }
        switch ($options['type']) {
            case 'checkbox':
                if (!isset($options['inline'])) {
                    $options['inline'] = $this->checkClasses($options['type'] . '-inline', (array) $options['label']);
                }
                if ($options['inline']) {
                    $options['label'] = $this->injectClasses($options['type'] . '-inline', (array) $options['label']);
                    if (!isset($newTemplates['checkboxContainer'])) {
                        $options['templates']['checkboxContainer'] = '{{content}}';
                    }
                }
                unset($options['inline']);
                break;
            case 'radio':
                if ($options['inline'] && $this->_align !== 'horizontal') {
                    $options['templates']['formGroup'] = $this->templater()->get('radioInlineFormGroup');
                }
                if (!$options['inline']) {
                    $options['templates']['nestingLabel'] = $this->templater()->get('radioNestingLabel');
                }
                break;
            case 'select':
                if (isset($options['multiple']) && $options['multiple'] === 'checkbox') {
                    $options['type'] = 'multicheckbox';
                }
                break;
            case 'multiselect':
                break;
            case 'textarea':
            default:
                if ($options['label'] !== false && strpos($this->templates('label'), 'class=') === false) {
                    $options['label'] = $this->injectClasses('control-label', (array) $options['label']);
                }
        }
        if ($options['help']) {
            $options['help'] = $this->templater()->format('help', ['content' => $options['help']]);
        }
        $result = parent::input($fieldName, $options);
        if ($newTemplates) {
            $this->templater()->pop();
        }
        return $result;
    }

Usage Example

Пример #1
0
 public function testHelpTextHorizontal()
 {
     $this->Form->create($this->article, ['align' => 'horizontal']);
     $result = $this->Form->input('title', ['help' => 'help text']);
     $expected = ['div' => ['class' => 'form-group text required'], 'label' => ['class' => 'control-label col-md-2', 'for' => 'title'], 'Title', '/label', ['div' => ['class' => 'col-md-6']], 'input' => ['type' => 'text', 'name' => 'title', 'id' => 'title', 'class' => 'form-control', 'required' => 'required'], ['div' => ['class' => 'help-block']], 'help text', '/div', '/div', '/div'];
     $this->assertHtml($expected, $result);
     $result = $this->Form->input('published', ['help' => 'help text']);
     $expected = ['div' => ['class' => 'form-group checkbox'], ['div' => ['class' => 'col-md-offset-2 col-md-6']], ['div' => ['class' => 'checkbox']], 'input' => ['type' => 'hidden', 'name' => 'published', 'value' => 0], 'label' => ['for' => 'published'], ['input' => ['type' => 'checkbox', 'name' => 'published', 'id' => 'published', 'value' => 1]], 'Published', '/label', '/div', ['div' => ['class' => 'help-block']], 'help text', '/div', '/div', '/div'];
     $this->assertHtml($expected, $result);
     $this->article['errors'] = ['title' => ['error message']];
     $this->Form->create($this->article, ['align' => 'horizontal']);
     $result = $this->Form->input('title', ['help' => 'help text']);
     $expected = ['div' => ['class' => 'form-group text required has-error'], 'label' => ['class' => 'control-label col-md-2', 'for' => 'title'], 'Title', '/label', ['div' => ['class' => 'col-md-6']], 'input' => ['type' => 'text', 'name' => 'title', 'id' => 'title', 'class' => 'form-control', 'required' => 'required'], ['div' => ['class' => 'help-block']], 'error message', '/div', ['div' => ['class' => 'help-block']], 'help text', '/div', '/div', '/div'];
     $this->assertHtml($expected, $result);
 }