【问题标题】:Site wide Zend_Form站点范围 Zend_Form
【发布时间】:2009-10-05 15:44:12
【问题描述】:

如何向我的 layout.phtml 添加表单?

我希望能够在我的网站上的每个表单中都保留一个搜索表单和登录表单。

【问题讨论】:

    标签: php zend-framework forms zend-form


    【解决方案1】:

    我有一篇博文对此进行了解释:http://blog.zero7ict.com/2009/11/how-to-create-reusable-form-zend-framework-zend_form-validation-filters/

    在您的 Application 文件夹中创建一个 Forms 文件夹

    这是一个示例表单:

    <?php
    class Form_CreateEmail extends Zend_Form
    {
    public function __construct($options = null)
    {
        parent::__construct($options);
    
        $this->setName('createemail');
        $title = new Zend_Form_Element_Text('title');
        $title->setLabel('Subject')
        ->setRequired(true)
        ->addFilter('StripTags')
        ->addFilter('StringTrim')
        ->addValidator('NotEmpty');
        $info = new Zend_Form_Element_Textarea('info');
        $info->setLabel('Email Content')
        ->setAttribs(array('rows' => 12, 'cols' => 79)); 
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setAttrib('id', 'submitbutton');
        $this->addElements(array($title, $info, $submit));
    }
    
    }
    ?>
    

    然后您可以像这样从控制器中调用它:

    $form = new Form_CreateEmail();
            $form->submit->setLabel('Add');
            $this->view->form = $form;
    

    并从您的视图中显示它使用

    echo $this->form;
    

    希望这会有所帮助。

    编辑:如果你想让它包含在每个页面中,你可以创建一个新的帮助文件

    在你的views文件夹中创建一个helpers文件夹并创建一个loginHelper.php文件

    class Zend_View_Helper_LoginHelper
    {
        function loginHelper()
        {
    
    $form = new Form_CreateEmail();
            $form->submit->setLabel('Add');
            return = $form;
    
        }
    }
    

    这可以从您的布局中使用:

    <?php echo $this->LoginHelper(); ?>     
    

    【讨论】:

    • 请遵守编码标准!你的班级应该是 Form_CreateEmail ! (如 Zend_View_Helper_LoginHelper)
    • 方法init()的存在是为了不用重载__construct($options)
    【解决方案2】:

    在你的布局中做:

    $form = new Loginform();
    echo $form->render();
    

    您只需确保为要 POST 的表单指定一个控制器/操作,这样就不会 POST 到您当前使用的任何控制器,这是默认行为。

    【讨论】:

      猜你喜欢
      • 2014-12-10
      • 2012-06-26
      • 2012-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-23
      • 1970-01-01
      相关资源
      最近更新 更多