我正在尝试删除隐藏表单元素上的默认装饰器。默认情况下,隐藏元素显示如下:

<dt>Hidden Element Label (if I had set one)</dt>
<dd><input type="hidden" name="foobar" value="1" id="foobar"></dd>

我不希望隐藏元素占用页面上的空间。我想删除所有默认装饰器,所以我只剩下输入标签。

<input type="hidden" name="foobar" value="1" id="foobar">

我怎样才能实现这个目标?

有帮助吗?

解决方案

有关隐藏字段,你只需要一个装饰 - 视图助手:

$field = new Zend_Form_Element_Hidden('id');
$field->setDecorators(array('ViewHelper'));

这将使得只有输入字段,而不DT-DD包装和标签。

其他提示

Zend的元素装饰文件:

  

默认装饰难道不需要   装载

     

默认情况下,缺省的装饰是   对象的初始化过程中加载。   你可以通过禁用该   “disableLoadDefaultDecorators”选项   给构造:

$element = new Zend_Form_Element('foo', 
    array('disableLoadDefaultDecorators' => true)
);

我使用此

$element->removeDecorator('DtDdWrapper');

摆脱围绕特定元件DT DD标签

//基于上文 - 一个简单的函数来隐藏元素添加到$这种形式

/**
 * Add Hidden Element
 * @param $field
 * @param value
 * @return nothing - adds hidden element
 * */
public function addHid($field, $value){     
    $hiddenIdField = new Zend_Form_Element_Hidden($field);
    $hiddenIdField->setValue($value)
          ->removeDecorator('label')
          ->removeDecorator('HtmlTag');     
    $this->addElement($hiddenIdField);
}

当你有很多的隐性投入最好的答案是这样的:

$elements = $this->getElements();
foreach ($elements as $elem)
    if ($elem instanceof Zend_Form_Element_Hidden)
        $elem->removeDecorator('label')->removeDecorator('HtmlTag');

正如其他职位提到setDisableLoadDefaultDecorators(true)不一样,如果他们已经加载工作...但clearDecorators()呢!

我不能让disableLoadDefaultDecorators工作完全正确。这是我想出了一个解决方案。

$hiddenIdField = new Zend_Form_Element_Hidden('id');
$hiddenIdField->setValue($portalId)
              ->removeDecorator('label')
              ->removeDecorator('HtmlTag'); 

在HTML中,隐藏字段出现的周围没有任何额外的代码。

...
<dt><label for="password" class="required">Password</label></dt>
<dd><input type="password" name="password" id="password" value="" /></dd>
<input type="hidden" name="id" value="1" id="id" />
...

下面是从 http://www.phpfreaks.com/什么takeme2web论坛/的index.php?主题= 225848.0 表明

$ yourhiddenzendformelement-> setDecorators(阵列( '视图助手'));

如果您仍在使用,仅使用单个“ViewHelper”装饰器将生成无效标记 <dl> 包装纸。另一种方法概述于 ZF-2718. 。这会将隐藏字段添加到包装在 <dd>.

好了,2012还是一样的问题。如果去掉装饰,在html不会验证。如果离开他们,隐藏要素占用空间。在我所有的项目我有一个CSS帮手.hidden,所以我只是将它应用到<dd>并取消设置标签:

$element = new Zend_Form_Element_Hidden('foo', array('value' => 'bar'));
$element->removeDecorator('Label');
$element->getDecorator('HtmlTag')->setOption('class', 'hidden');

有效的HTML(5),美观的形式。这也可以进入的定制的装饰为隐藏的字段。

修改

这是我怎么把它变成我自己的表单元素:

class Exanto_Form_Element_Hidden extends Zend_Form_Element_Hidden
{
    public function render(Zend_View_Interface $view = null)
    {
        $this->removeDecorator('Label');
        $this->getDecorator('HtmlTag')->setOption('class', 'hidden');
        return parent::render($view);
    }
}

使用这样的:

    foreach ($this->getElements() as $element) {

        $decorator = $element->getDecorator('label');
        if (!$decorator) {
            continue;
        }
        $decorator->removeOption('tag');
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top