2011-02-03 6 views
1

のZend_Formの結果により、いくつかのテキストを折り返すことができます。どのように私は結果のZend_Formのをレンダリングすることにより、私のテキストを折り返す必要レンダリング

あなたはコードですべてを理解することができるようになります(私の英語の悪い、ので)(このサンプルではこれだけですが)は動作しません:

$HTML = $Form -> render(new Zend_View('asdasd')); 

私が取得することになって:

<form> 
asdasd 
</form> 
+0

あなたがしたいことはわかりません。 Zend_Viewは文字列ではなく$ config配列をとります。あなたは何をしたいのかを明確にしようとしますか? – Marcin

+0

http://framework.zend.com/manual/en/learning.form.decorators.simplest.htmlしかしそれほどエレガントではない –

答えて

0

あなたは簡単にこの

のように、コントローラからフォームをレンダリングすることができますあなたのコントローラで、コントローラ

$form = new Zend_Form(); 
//Create your form elements 

$this->view->form = $form 

echo $this->form 

Zend_Formオブジェクトは、単純にエコーを使って、あなたのためのフォームをレンダリングします__toString()方法があります。

1

あなたがしていることを(私が正しく理解していれば)あなたがしようとしていることは、カスタムのフォームビューヘルパーを書くことによって達成できると思います。このため、私はそれをどうやってできるかというドラフトを用意しました。これが最善の方法であるかどうかはわかりませんが、うまくいくはずです。

まず、カスタムフォームビューヘルパーを作成します。デフォルト値はZend_View_Helper_Formです。 Zend_FormはレンダリングにZend_Form_Decorator_Formを使用しています。実際にはZend_View_Helper_Formを使ってフォームのxhtml表現を生成しています。したがって、独自のフォームヘルパーを作成することができます。どこかBootsrap.phpで

// Example file location: APPLICATION_PATH/views/helpers/CustomForm.php 
class My_View_Helper_CustomForm extends Zend_View_Helper_Form { 


    /** 
    * Render HTML form 
    * 
    * @param string $name Form name 
    * @param null|array $attribs HTML form attributes 
    * @param false|string $content Form content 
    * @return string 
    */ 
    public function customForm($name, $attribs = null, $content = false) 
    { 

     // THE FOLLOWING FEW LINES ARE NEW. 
     // get myText and unset it from $attribs afterwards.   
     $myText = '';   
     if (array_key_exists('myText', $attribs)) { 
      $myText = $attribs['myText']; 
      unset($attribs['myText']); 
     } 

     // this is from orginal form view helper 
     $info = $this->_getInfo($name, $content, $attribs); 
     extract($info); 

     if (!empty($id)) { 
      $id = ' id="' . $this->view->escape($id) . '"'; 
     } else { 
      $id = ''; 
     } 

     if (array_key_exists('id', $attribs) && empty($attribs['id'])) { 
      unset($attribs['id']); 
     } 

     $xhtml = '<form' 
       . $id 
       . $this->_htmlAttribs($attribs) 
       . '>'; 


     // THE NEXT LINE IS AGAIN NEW   
     // WE PUT $myText after opening <form> tag (as in your example). 
     $xhtml .= $myText . 'sadfsf'; 



     // the rest of the form, as usuall. 
     if (false !== $content) { 
      $xhtml .= $content 
        . '</form>'; 
     } 

     return $xhtml; 
    } 

} 

を使用すると、オートローダにビューヘルパーのローカリゼーションを追加する必要があり、次のようZend_View_Helper_Formを拡張My_View_Helper_CustomFormと呼ばれます。 Zend_Viewにビューヘルパーディレクトリを追加することによっても可能です。それにもかかわらず、私は次のようにちょうどそれがオートローダーを使用してください:

$autoLoader = Zend_Loader_Autoloader::getInstance(); 

    $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
       'basePath' => APPLICATION_PATH, 
       'namespace' => '', 
      )); 

    $resourceLoader->addResourceType('view', 'views/helpers/', 'My_View_Helper_'); 
    $autoLoader->pushAutoloader($resourceLoader); 

を私は上記のcustomFormを使用しますtestActionのコード例を示します、という終われ:

$myForm = new My_Form_SomeForm(); 

    // set your text you want to put in the form 
    // Just don't forget to unset it the new view helper. 
    $myForm->setAttrib('myText', 'asdasd'); 


    // get form decorator (i.e. Zend_Form_Decorator_Form) 
    $formDecorator = $myForm->getDecorator('form'); 

    // $formDecorator uses Form helper to actually cunstruct xhtml of the form. 
    // So we set our new helper (i.e. My_View_Helper_CustomForm) 
    // instead of default one (i.e. Zend_View_Helper_Form). 
    // setHelper() method takes only string, thus ZF will need to know how to find 
    // the new view helper. This was the reason I added it to the autoloader. 
    $formDecorator->setHelper('customForm'); 

    $HTML = $myForm->render(); 

希望これはに有用であろうあなたとあなたが問題を解決するのに役立ちます。

関連する問題