2011-09-29 120 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