Cake\View\Helper\FormHelper::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 = [])
    {
        $options += ['secure' => true];
        $options = $this->_initInputField($fieldName, $options);
        unset($options['type']);
        return $this->widget('file', $options);
    }

Usage Example

 /**
  * Creates file input widget.
  *
  * @param string $fieldName Name of a field, in the form "modelname.fieldname"
  * @param array $options Array of HTML attributes.
  * @return string A generated file input.
  * @link http://book.cakephp.org/3.0/en/views/helpers/form.html#creating-file-inputs
  */
 public function file($fieldName, array $options = [])
 {
     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 . "';"]));
     $fakeInputCustomOptions = $this->_extractOption('_input', $options, []);
     unset($options['_input']);
     $fakeButtonCustomOptions = $this->_extractOption('_button', $options, []);
     unset($options['_button']);
     $fakeInput = $this->text($fieldName, array_merge($options, $fakeInputCustomOptions, ['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, 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);
 }