yii\widgets\ActiveField::fileInput PHP Method

fileInput() public method

This method will generate the name and value tag attributes automatically for the model attribute unless they are explicitly specified in $options.
public fileInput ( array $options = [] )
$options array the tag options in terms of name-value pairs. These will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using [[Html::encode()]]. If you set a custom `id` for the input element, you may need to adjust the [[$selectors]] accordingly.
    public function fileInput($options = [])
    {
        // https://github.com/yiisoft/yii2/pull/795
        if ($this->inputOptions !== ['class' => 'form-control']) {
            $options = array_merge($this->inputOptions, $options);
        }
        // https://github.com/yiisoft/yii2/issues/8779
        if (!isset($this->form->options['enctype'])) {
            $this->form->options['enctype'] = 'multipart/form-data';
        }
        $this->adjustLabelFor($options);
        $this->parts['{input}'] = Html::activeFileInput($this->model, $this->attribute, $options);
        return $this;
    }

Usage Example

コード例 #1
0
 /**
  * @inheritdoc
  */
 public function fileInput($options = [])
 {
     $options = array_merge($this->inputOptions, $options);
     $options['wrapperOptions']['tag'] = isset($options['wrapperOptions']) && isset($options['wrapperOptions']['tag']) ? $options['wrapperOptions']['tag'] : 'div';
     $this->_setWrapperOptions($options, 'input input-file');
     // Set template
     $this->template = $this->fileInputTemplate;
     // Button
     $btnOptions = ArrayHelper::remove($options, 'buttonOptions', []);
     $btnTag = ArrayHelper::remove($btnOptions, 'tag', 'span');
     $btnContent = ArrayHelper::remove($btnOptions, 'content', 'Browse');
     Html::addCssClass($btnOptions, 'button');
     $this->parts['{beginButton}'] = Html::beginTag($btnTag, $btnOptions);
     $this->parts['{contentButton}'] = $btnContent;
     $this->parts['{endButton}'] = Html::endTag($btnTag);
     // Placeholder input
     $phrOptions = ArrayHelper::remove($options, 'placeholderOptions', []);
     $phrOptions['readonly'] = 'readonly';
     $placeholder = ArrayHelper::remove($options, 'placeholder');
     if ($placeholder !== null) {
         $phrOptions['placeholder'] = $placeholder;
     }
     $this->parts['{placeholder}'] = Html::input('text', null, null, $phrOptions);
     // Onchange
     $onchange = 'this.parentNode.nextSibling.value = this.value';
     $options['onchange'] = isset($options['onchange']) ? $options['onchange'] : $onchange;
     return parent::fileInput($options);
 }
All Usage Examples Of yii\widgets\ActiveField::fileInput