Zend框架如何在表单中维护以前的值

时间:2014-01-09 08:41:50

标签: php mysql zend-framework

我正在使用zend表单和表单装饰器来查看表单。 提交此表单后,如果有任何错误(验证),我的表单将变为空白。

如何恢复表单中的先前值。如果成功提交,表单应该为空白。

articleForm.php

<?php
class Application_Form_articleForm extends Zend_Form
{
   public function init()
   {
       $this->setMethod('post');      
       //$id = $this->createElement('hidden','id');
       $name = $this->createElement('text','name');
       $name->setLabel('URL name:')
                   ->setAttrib('size',250);
       $title = $this->createElement('text','title');
       $title->setLabel('Title:')
                   ->setAttrib('size',250);
       $group_id = $this->createElement('select','group_id');
       $group_id->setLabel('Category:')
                   ->addMultiOptions(array(
                    'US' => 'United States',
                    'UK' => 'United Kingdom' 
                        ));  
       $tags = $this->createElement('text','tags');
       $tags->setLabel('Tags:')
                   ->setAttrib('size',250);
       $status = $this->createElement('text','status');
       $status->setLabel('status:')
                   ->setAttrib('size',250);

       //$Publish = $this->createElement('submit','Publish');
       $Publish = new Zend_Form_Element_Submit('Publish');
       $Publish->setLabel("Publish")
               ->setIgnore(true);

       $allowed_tags = array(
    'a' =>  array('href', 'title'),
    'strong',
    'img'   => array('src', 'alt'),
    'ul',
    'ol',
    'li',
    'em',
    'u',
    'strike');

$content = new Zend_Form_Element_Textarea('content');
$content->setLabel('content')
    ->setAttrib('rows', 12)
    ->setAttrib('cols', 40)
    ->setRequired(true)
    ->addFilter('StringTrim')
    ->addFilter('StripTags', $allowed_tags);

$this->addElements(array(
           $name,
           $title,
           $group_id,
           $tags,
           $content, 
           $status,
           $Publish
       ));

      $this->setDecorators(array(array('viewScript', array('viewScript' => 'admin/articleFormDecorator.phtml'))));
   }
}

adminController.php indexsAction()

public function indexAction() {
        $mysession = new Zend_Session_Namespace('Admin');
        if (!isset($mysession->adminName)) {
            $this->_redirect('/admin/login');
        } 
        $form = new Application_Form_articleForm();
        $this->view->form = $form; 
        $content  = new Application_Model_Content();
        if ($this->_request->getPost('Publish')) {
            $formData = $this->_request->getPost();           
            if ($form->isValid($formData)) { //If form data is valid   
                $content->insert($formData);                
            }
        }
    }

我也得到了以下错误 例外信息:

消息:SQLSTATE [42S22]:找不到列:1054“字段列表”中的未知列“发布”

发布提交按钮的位置。

1 个答案:

答案 0 :(得分:1)

请将您的操作修改为 你需要添加unset($ formData ['Publish']);

 public function indexAction() {
        $mysession = new Zend_Session_Namespace('Admin');
        if (!isset($mysession->adminName)) {
            $this->_redirect('/admin/login');
        } 
        $form = new Application_Form_articleForm();
        $this->view->form = $form; 
        $content  = new Application_Model_Content();
        //$data = $content->fetchAll($content->select());
        if ($this->_request->getPost('Publish')) {
            $formData = $this->_request->getPost();           
            if ($form->isValid($formData)) { //If form data is valid
                unset($formData['Publish']);
                $content->insert($formData);
            }
        }
    }