【问题标题】:Difficult decorators for file input文件输入的困难装饰器
【发布时间】:2017-03-19 19:54:38
【问题描述】:

我需要这个标记来输入文件:

<label class="col-sm-12">File upload</label>
<div class="col-sm-12">
  <div class="fileinput fileinput-new input-group" data-provides="fileinput">
    <div class="form-control" data-trigger="fileinput">
      <i class="glyphicon glyphicon-file fileinput-exists"></i>
      <span class="fileinput-filename"></span>
    </div>
    <span class="input-group-addon btn btn-default btn-file">
      <span class="fileinput-new">Select file</span>
      <span class="fileinput-exists">Change</span>
      <input type="file" name="...">
    </span>
    <a href="#" class="input-group-addon btn btn-default fileinput-exists" data-dismiss="fileinput">Remove</a
  </div>
  <ul class="errors">
    <li>Some error</li>
  </ul>
</div>

我是这样尝试的:

<label class="col-sm-12">Attachment</label>
<div class="col-sm-12">
  <?php echo $this->form->attachment; ?>
</div>

使用非常困难的装饰器:

$this->fileDecorator = array(
  array(
    array('divOpen' => 'HtmlTag'), array('tag' => 'div', 'class' => 'form-control', 'data-trigger' => 'fileinput', 'openOnly' => true, 'placement' => Zend_Form_Decorator_Abstract::APPEND)
  ),
  array(
    array('i' => 'HtmlTag'), array('tag' => 'i', 'class' => 'glyphicon glyphicon-file fileinput-exists', 'placement' => Zend_Form_Decorator_Abstract::APPEND)
  ),
  array(
    array('span' => 'HtmlTag'), array('tag' => 'span', 'class' => 'fileinput-filename', 'placement' => Zend_Form_Decorator_Abstract::APPEND)
  ),
  array(
    array('divClose' => 'HtmlTag'), array('tag' => 'div', 'closeOnly' => true, 'placement' => Zend_Form_Decorator_Abstract::APPEND)
  ),
  array(
    array('spanOpen' => 'HtmlTag'), array('tag' => 'span', 'class' => 'input-group-addon btn btn-default btn-file', 'openOnly' => true, 'placement' => Zend_Form_Decorator_Abstract::APPEND)
  ),
  array(
    'Callback',
    array('callback' => 
      function($content, $element, $options) {
        return "<span class=\"{$options['class']}\">{$options['text']}</span><span class=\"{$options['class2']}\">{$options['text2']}</span>";
      },
      'class' => 'fileinput-new',
      'text' => $this->translator->_('_selectFile'),
      'class2' => 'fileinput-exists',
      'text2' => $this->translator->_('_change')
    )
  ),
  'File',
  array(
      array('spanClose' => 'HtmlTag'), array('tag' => 'span', 'closeOnly' => true, 'placement' => Zend_Form_Decorator_Abstract::APPEND)
  ),
  array(
    'Callback',
    array('callback' => 
      function($content, $element, $options) {
        return "<a href=\"#\" class=\"{$options['class']}\" data-dismiss=\"{$options['data-dismiss']}\">{$options['text']}</a>";
      },
      'class' => 'input-group-addon btn btn-default fileinput-exists',
      'text' => $this->translator->_('_remove'),
      'data-dismiss' => 'fileinput'
    )
  ),
  array(
    array('div' => 'HtmlTag'), array('tag' => 'div', 'class' => 'fileinput fileinput-new input-group', 'data-provides' => 'fileinput')
  ),
  'Errors'
);

但问题是,Callback 在装饰器中只能出现一次(不像 HtmlTag)。并且没有回调就不可能添加带有内容的标签。可以通过更多回调或不同方式解决吗?

编辑: 我有想法。是否可以将文件装饰器添加到回调装饰器?

【问题讨论】:

    标签: php zend-framework zend-form zend-decorators


    【解决方案1】:

    就这么简单:

    <div class="form-group<?php echo count($this->form->attachment->getErrors()) ? ' has-error has-feedback' : null; ?>">
      <label class="col-sm-12"><?php echo $this->form->attachment->renderLabel(); ?></label>
        <div class="col-sm-12">
          <div class="fileinput fileinput-new input-group" data-provides="fileinput">
            <div class="form-control" data-trigger="fileinput">
              <i class="glyphicon glyphicon-file fileinput-exists"></i>
              <span class="fileinput-filename"></span>
            </div>
            <span class="input-group-addon btn btn-default btn-file">
              <span class="fileinput-new">Select file</span>
              <span class="fileinput-exists">Change</span>
              <?php echo $this->form->attachment->renderFile(); ?>
            </span>
            <a href="#" class="input-group-addon btn btn-default fileinput-exists" data-dismiss="fileinput">Remove</a>
          </div>
          <?php echo $this->formErrors($this->form->attachment->getMessages()); ?>
        </div>
      </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 2011-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多