我正在使用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“字段列表”中的未知列“发布”

“发布是提交”按钮。

最佳答案

请修改您的操作为
您需要添加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);
            }
        }
    }

关于php - Zend框架如何保持形式的先前值(value),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21015293/

10-11 06:10