duanjiao5543 2014-01-09 08:41
浏览 28
已采纳

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

I am using zend form and form decorator to view form. After submitting this form and there is any error(validation) my form gets blank.

How can I restore previous values in form. And form should get blank if it gets submitted successfully.

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);                
            }
        }
    }

I am getting following error too
Exception information:

Message: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Publish' in 'field list'

Where Publish is submit button.

  • 写回答

1条回答 默认 最新

  • dqs86517 2014-01-09 10:30
    关注

    Please modify your action as You need to add 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);
                }
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化