简体   繁体   中英

Zend framework how to maintain previous value in form

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM