如何在 Zend_Form 文件元素上使用 ViewScripts? [英] How do I use ViewScripts on Zend_Form File Elements?

查看:18
本文介绍了如何在 Zend_Form 文件元素上使用 ViewScripts?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将此 ViewScript 用于我的标准表单元素:

<div class="field" id="field_<?php echo $this->element->getId(); ?>"><?php if (0 < strlen($this->element->getLabel())) : ?><?php echo $this->formLabel($this->element->getName(), $this->element->getLabel());?><?php endif;?><span class="value"><?php echo $this->{$this->element->helper}($this->element->getName(),$this->element->getValue(),$this->element->getAttribs()) ?></span><?php if (0 < $this->element->getMessages()->length) : ?><?php echo $this->formErrors($this->element->getMessages());?><?php endif;?><?php if (0 < strlen($this->element->getDescription())) : ?><span class="hint"><?php echo $this->element->getDescription();?></span><?php endif;?>

尝试单独使用该 ViewScript 会导致错误:

<块引用>

表单捕获的异常:无文件发现装饰器...无法渲染文件元素

查看 这个常见问题 揭示了我的部分问题,我更新了我的像这样的表单元素装饰器:

'decorators' =>大批(数组('文件'),数组('ViewScript', array('viewScript' => 'form/field.phtml')))

现在它渲染文件元素两次,一次是在我的视图脚本中,另一个是在我的视图脚本之外的文件元素:

<input type="hidden" name="UPLOAD_IDENTIFIER" value="4b5f7335a55ee" id="progress_key"/><input type="file" name="upload_file" id="upload_file"/><div class="field" id="field_upload_file"><label for="upload_file">上传文件</label><span class="value"><input type="file" name="upload_file" id="upload_file"/></span>

有关如何使用 ViewScript 正确处理此问题的任何想法?

<小时>

更新:基于肖恩的解决方案,这是我的最终代码:

表单元素:

$this->addElement('file', 'upload_file', array('disableLoadDefaultDecorators' =>真的,'装饰器' =>数组('文件',数组('ViewScript',数组('viewScript' =>'_form/file.phtml','位置' =>错误的,))),'标签' =>'上传','必需' =>错误的,'过滤器' =>大批(),'验证器' =>数组(数组('计数',假,1),),));

查看脚本:

element->getType()));if ($this->element->isRequired()) {$class .= '需要';}if ($this->element->hasErrors()) {$class .=' 错误';}?><div class="<?php echo $class; ?>"id="field_<?php echo $this->element->getId(); ?>"><?php if (0 < strlen($this->element->getLabel())): ?><?php echo $this->formLabel($this->element->getFullyQualifiedName(), $this->element->getLabel());?><?php endif;?><span class="value"><?php echo $this->content;?></span><?php if ($this->element->hasErrors()): ?><?php echo $this->formErrors($this->element->getMessages());?><?php endif;?><?php if (0 < strlen($this->element->getDescription())): ?><p class="hint"><?php echo $this->element->getDescription();?></p><?php endif;?>

解决方案

答案其实比较简单.您需要做的就是首先指定文件装饰器,为文件输入创建一个特定的视图脚本,并在 viewScript 装饰器参数中为放置指定 false,这将有效地将文件装饰器的输出注入到 viewScript 装饰器中,例如

$element->setDecorators(array('File', array('ViewScript', array('viewScript' => 'decorators/file.phtml', 'placement' => false))));

然后在新的文件元素视图脚本中,您只需在要放置文件输入标记的脚本中回显 $this->content.这是最近项目中的一个例子,所以如果它看起来有点奇怪,请忽略标记,希望它能说明这一点.

渲染时你会看到这个元素的html

I am using this ViewScript for my standard form elements:

<div class="field" id="field_<?php echo $this->element->getId(); ?>">
   <?php if (0 < strlen($this->element->getLabel())) : ?>
      <?php echo $this->formLabel($this->element->getName(), $this->element->getLabel());?>
   <?php endif; ?>
   <span class="value"><?php echo $this->{$this->element->helper}(
      $this->element->getName(),
      $this->element->getValue(),
      $this->element->getAttribs()
   ) ?></span>
   <?php if (0 < $this->element->getMessages()->length) : ?>
       <?php echo $this->formErrors($this->element->getMessages()); ?>
   <?php endif; ?>
   <?php if (0 < strlen($this->element->getDescription())) : ?>
      <span class="hint"><?php echo $this->element->getDescription(); ?></span>
   <?php endif; ?>
</div>

Trying to use that ViewScript alone results in an error:

Exception caught by form: No file decorator found... unable to render file element

Looking at this FAQ revealed part of my problem, and I updated my form element decorators like this:

'decorators' => array(
   array('File'),
   array('ViewScript', array('viewScript' => 'form/field.phtml'))
)

Now it's rendering the file element twice, once within my view script, and extra elements with the file element outside my view script:

<input type="hidden" name="MAX_FILE_SIZE" value="8388608" id="MAX_FILE_SIZE" />
<input type="hidden" name="UPLOAD_IDENTIFIER" value="4b5f7335a55ee" id="progress_key" />
<input type="file" name="upload_file" id="upload_file" />
<div class="field" id="field_upload_file">
    <label for="upload_file">Upload File</label>
    <span class="value"><input type="file" name="upload_file" id="upload_file" /></span>
</div>

Any ideas on how to handle this properly with a ViewScript?


UPDATE: Based on Shaun's solution, here's my final code:

Form Element:

$this->addElement('file', 'upload_file', array(
    'disableLoadDefaultDecorators' => true,
    'decorators' => array('File', array('ViewScript', array(
        'viewScript' => '_form/file.phtml',
        'placement' => false,
    ))),
    'label' => 'Upload',
    'required' => false,
    'filters' => array(),
    'validators' => array(array('Count', false, 1),),
));

View Script:

<?php
$class .= 'field ' . strtolower(end(explode('_',$this->element->getType())));
if ($this->element->isRequired()) {
    $class .= ' required';
}
if ($this->element->hasErrors()) {
    $class .= ' errors';
}
?>
<div class="<?php echo $class; ?>" id="field_<?php echo $this->element->getId(); ?>">
    <?php if (0 < strlen($this->element->getLabel())): ?>
        <?php echo $this->formLabel($this->element->getFullyQualifiedName(), $this->element->getLabel());?>
    <?php endif; ?>
    <span class="value"><?php echo $this->content; ?></span>
    <?php if ($this->element->hasErrors()): ?>
        <?php echo $this->formErrors($this->element->getMessages()); ?>
    <?php endif; ?>
    <?php if (0 < strlen($this->element->getDescription())): ?>
        <p class="hint"><?php echo $this->element->getDescription(); ?></p>
    <?php endif; ?>
</div>

解决方案

The answer is relatively simple as it happens. All you need do is specify the File decorator first, create a specific view script for the file input and specify false for the placement in the viewScript decorator arguments, this will effectively inject the output from the File decorator into the viewScript decorator e.g.

$element->setDecorators(array('File', array('ViewScript', array('viewScript' => 'decorators/file.phtml', 'placement' => false))));

Then in the new file element view script you simply echo $this->content in the script where you'd like the file input markup to be placed. Here's an example from a recent project, so ignore the markup if it looks a little odd, hopefully it will illustrate the point.

<label for="<?php echo $this->element->getName(); ?>" class="element <?php if ($this->element->hasErrors()): ?> error<?php endif; ?>" id="label_<?php echo $this->element->getName(); ?>"> 
<span><?php echo $this->element->getLabel(); ?></span>

<?php echo $this->content; ?>

<?php if ($this->element->hasErrors()): ?>

    <span class="error">
        <?php echo $this->formErrors($this->element->getMessages()); ?>
    </span>

<?php endif; ?>

</label>

When rendered you will see this html for the element

<label for="photo" class="element" id="label_photo"> 
<span>Photo</span>

<input type="hidden" name="MAX_FILE_SIZE" value="6291456" id="MAX_FILE_SIZE">
<input type="file" name="photo" id="photo">

</label>

这篇关于如何在 Zend_Form 文件元素上使用 ViewScripts?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆