Bootstrap\View\Helper\BootstrapFormHelper::file PHP Метод

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

Creates file input widget.
public file ( string $fieldName, array $options = [] ) : string
$fieldName string Name of a field, in the form "modelname.fieldname"
$options array Array of HTML attributes.
Результат string A generated file input.
    public function file($fieldName, array $options = [])
    {
        if (!$this->config('useCustomFileInput') || isset($options['default']) && $options['default']) {
            return parent::file($fieldName, $options);
        }
        $options += ['_input' => [], '_button' => [], 'id' => $fieldName, 'secure' => true, 'count-label' => __('files selected'), 'button-label' => __('Choose File')];
        $fakeInputCustomOptions = $options['_input'];
        $fakeButtonCustomOptions = $options['_button'];
        unset($options['_input'], $options['_button']);
        $options = $this->_initInputField($fieldName, $options);
        unset($options['type']);
        $countLabel = $options['count-label'];
        unset($options['count-label']);
        $fileInput = $this->widget('file', array_merge($options, ['style' => 'display: none;', 'onchange' => "document.getElementById('" . $options['id'] . "-input').value = (this.files.length <= 1) ? this.files[0].name : this.files.length + ' ' + '" . $countLabel . "';", 'escape' => false]));
        $fakeInputCustomOptions += ['value' => $options['val']['name']];
        $fakeInput = $this->text($fieldName, array_merge($fakeInputCustomOptions, ['name' => $fieldName . '-text', 'readonly' => 'readonly', 'id' => $options['id'] . '-input', 'onclick' => "document.getElementById('" . $options['id'] . "').click();", 'escape' => false]));
        $buttonLabel = $options['button-label'];
        unset($options['button-label']);
        $fakeButton = $this->button($buttonLabel, array_merge($fakeButtonCustomOptions, ['type' => 'button', 'onclick' => "document.getElementById('" . $options['id'] . "').click();"]));
        return $fileInput . $this->Html->div('input-group', $this->Html->div('input-group-btn', $fakeButton) . $fakeInput);
    }

Usage Example

Пример #1
0
 public function file($fieldName, array $options = [])
 {
     $this->_customFileInput = true;
     if (!$this->_customFileInput || isset($options['default']) && $options['default']) {
         return parent::file($fieldName, $options);
     }
     if (!isset($options['id'])) {
         $options['id'] = $fieldName;
     }
     $options += ['secure' => true];
     $options = $this->_initInputField($fieldName, $options);
     unset($options['type']);
     $countLabel = $this->_extractOption('count-label', $options, __('files selected'));
     unset($options['count-label']);
     $fileInput = $this->widget('file', array_merge($options, ['style' => 'display: none;', 'onchange' => "document.getElementById('" . $options['id'] . "-input').value = (this.files.length <= 1) ? this.files[0].name : this.files.length + ' ' + '" . $countLabel . "';"]));
     $fakeInput = $this->text($fieldName, array_merge($options, ['readonly' => 'readonly', 'id' => $options['id'] . '-input', 'onclick' => "document.getElementById('" . $options['id'] . "').click();"]));
     $buttonLabel = $this->_extractOption('button-label', $options, __('Choose File'));
     unset($options['button-label']);
     $fakeButton = $this->button($buttonLabel, ['icon' => 'upload', 'type' => 'button', 'onclick' => "document.getElementById('" . $options['id'] . "').click();"]);
     return $fileInput . $this->Html->div('input-group', $this->Html->div('input-group-btn', $fakeButton) . $fakeInput);
 }