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.
Пример #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);
 }