2011-09-29 130 views
4

我宁愿不处理装饰器,因为我的表单设计并不是直截了当,但我想保留验证表单的功能。Zend子窗体视图脚本渲染

所以我建立了子窗体工作正常的地方,但是当我尝试在我的视图中手动设置样式时,我得到了没有父窗体的名称。我见过其他类似的帖子,但我还没有找到解决方案。

例子:

这在我看来是脚本

<?php echo $this->form->username->renderViewHelper();?>

然后我得到

<input type="text" value="" id="username" name="username">

当呈现。它应该是

<input type="text" value="" id="form1-username" name="form1[username]">

我如何获取form1的部分?

谢谢!


编辑

好了,我找到了一个途径。

通过使用belongsTo关系,它的工作原理:

$form1->addElements(array(
     new Zend_Form_Element_Text('username', array(
      'belongsTo' => 'form1', 
      'required' => true, 
      'label'  => 'Username:', 
      'filters' => array('StringTrim', 'StringToLower'), 
      'validators' => array(
       'Alnum', 
       array('Regex', 
         false, 
         array('/^[a-z][a-z0-9]{2,}$/')) 
      ) 
     )) 
    )); 

是否有更好的方法来做到这一点或这是唯一的办法?


EDIT2

public function prepareSubForm($spec){ 
    if (is_string($spec)) { 
     $subForm = $this->{$spec}; 
    } elseif ($spec instanceof Zend_Form_SubForm) { 
     $subForm = $spec; 
    } else { 
     throw new Exception('Invalid argument passed to ' . 
          __FUNCTION__ . '()'); 
    } 
    $this->setSubFormDecorators($subForm) 
     ->addSubmitButton($subForm) 
     ->addSubFormActions($subForm); 
    return $subForm; 
} 

public function setSubFormDecorators(Zend_Form_SubForm $subForm){ 
    $subForm->setDecorators(array(
     'FormElements', \\<--- I tried to change this to PrepareElements before. 
     array('HtmlTag', array('tag' => 'dl', 
           'class' => 'zend_form')), 
     'Form', 
    )); 
    return $this; 
} 
+1

的['PrepareElements'(http://framework.zend.com/manual/en/zend.form.standardDecorators.html#zend.form.standardDecorators.prepareElements)装饰者的形式? –

+0

[Zend:ViewScript装饰器和数组表示法]的完全重复(http://stackoverflow.com/questions/7155047/zend-viewscript-decorator-and-array-notation)。这是关于这个主题在过去几个小时发布的第二个问题,很奇怪 – Phil

+0

@fireeyedboy没有,不适更新我的帖子与我正在使用。我是Zend的新手,所以我不知道装饰者现在是如何工作的。 – Matt

回答

1

我相信你可以通过使用得到您想要的输出:

<?php echo $this->form->username; ?> 

调用此不renderViewHelper时,我得到预期的输出。这也没有任何装饰者或准备子表格的特殊代码。我所要做的就是将belongsTo添加到表单元素。

更新:

如果你设置这是默认的装饰器,你可以从渲染消除DD/dt的标签,而是会使用一个div。然后你可能会更接近你想要的自定义输出。您可以在HtmlTagtagdiv到任何标签,你想换你的元素,改变这是我用的大多是:

array(
    'ViewHelper', 
    'Errors', 
    array('Description', array('tag' => 'p', 'class' => 'description')), 
    array('HtmlTag', array('tag' => 'div', 'class' => 'form-div')), 
    array('Label', array('class' => 'form-label', 'requiredSuffix' => '*')) 
); 

这是Zend Framework的默认:

array(
    'ViewHelper', 
    'Errors', 
    array('Description', array('tag' => 'p', 'class' => 'description')), 
    array('HtmlTag', array('tag' => 'dd', 'id' => array('callback' => $getId))) 
    array('Label', array('tag' => 'dt')) 
); 

请注意,文件和提交/按钮元素使用不同的装饰器。

您正在使用另见this answer

+0

如果我做默认渲染就像我得到dt/dd标签,我显然不希望如果我想手动做。但是,正如我的编辑所述,如果我使用belongsTo,我可以做到这一点,但是我无法使用PrepareElements工作。 – Matt

+0

查看我的更新,它显示了如何轻松更改dd/dt标签。 – drew010

+0

是它仍然包装所有的元素在dd/dt标签,但它包装所有的元素在一个容器div与类form-div。所以我不知道。就像我所说的,我不知道这些装饰器是如何工作的。 – Matt