Zend形成重定向

时间:2013-10-24 14:36:28

标签: php forms zend-framework

我在zend中编写代码以从用户输入中捕获数据并将其保存在数据库中。我正在使用zend表单。要捕获的信息太多了,所以我想创建另一个表单来填写其他数据......如何在填写第一个表单后从一个表单重定向到另一个表单?

这是我的Forms,Indexcontroller和模型的代码

IndexController.php

   <?php

    class IndexController extends Zend_Controller_Action
     {

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $organization_details = new Application_Model_DbTable_OrganizationDetails();
        $this->view->organization_details= $organization_details->fetchAll();
    }

    public function addAction()
    {
        $form = new Application_Form_OrganizationDetails();

        $form->submit->setLabel('Next');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {                

                $Organization_Name = $form->getValue('Organization_Name');
                $Organization_Trading_Name =  $form->getValue('Organization_Trading_Name');
                $Region = $form->getValue('Region');
                $City = $form->getValue('City');
                $Physical_Address = $form->getValue('Physical_Address');
                $Physical_code = $form->getValue('Physical_code');
                $Postal_Address = $form->getValue('Postal_Address');
                $Postal_code = $form->getValue('Postal_code');
                $Telephone_Number = $form->getValue('Telephone_Number');
                $Fax_Number = $form->getValue('Fax_Number');
                $Cellular_Number = $form->getValue('Cellular_Number');
                $Organization_Email = $form->getValue('Organization_Email');
                $Organization_Website = $form->getValue('Organization_Website');                                

                $organization_details = new  Application_Model_DbTable_OrganizationDetails();
                $organization_details->addOrganizationDetails($Organization_Name, $Organization_Trading_Name, $Region, $City, $Physical_Address, $Physical_code, $Postal_Address, $Postal_code, $Telephone_Number,$Fax_Number, $Cellular_Number, $Organization_Email, $Organization_Website);

                $this->_helper->redirector('add');
            } else {
                $form->populate($formData);
            }
}
    }

    public function editAction()
    {
        $form = new Application_Form_OrganizationDetails();
        $form->submit->setLabel('Update'); //can set it here also in forms
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int)$form->getValue('id');
                $Organization_Name = $form->getValue('Organization_Name');
                $Organization_Trading_Name = $form->getValue('Organization_Trading_Name');
                $Region = $form->getValue('Region');
                $City = $form->getValue('City');
                $Physical_Address = $form->getValue('Physical_Address');
                $Physical_code = $form->getValue('Physical_code');
                $Postal_Address = $form->getValue('Postal_Address');
                $Postal_code = $form->getValue('Postal_code');
                $Telephone_Number = $form->getValue('Telephone_Number');
                $Fax_Number = $form->getValue('Fax_Number');
                $Cellular_Number = $form->getValue('Cellular_Number');
                $Organization_Email = $form->getValue('Organization_Email');
                $Organization_Website = $form->getValue('Organization_Website');
                $organization_details = new Application_Model_DbTable_OrganizationDetails();
                $organization_details->updateOrganizationDetails($id, $Organization_Name, $Organization_Trading_Name, $Region, $City, $Physical_Address, $Physical_code, $Postal_Address, $Postal_code, $Telephone_Number,$Fax_Number, $Cellular_Number, $Organization_Email, $Organization_Website);

                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $organization_details = new Application_Model_DbTable_OrganizationDetails();
                $form->populate($organization_details->getOrganizationDetails($id));
            }
        }
    }

    public function deleteAction()
    {
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') {
                $id = $this->getRequest()->getPost('id');
                $organization_details = new Application_Model_DbTable_OrganizationDetails();
                $organization_details->deleteOrganizationDetails($id);
            }
            $this->_helper->redirector('index');
        } else {
            $id = $this->_getParam('id', 0);
            $organization_details = new Application_Model_DbTable_OrganizationDetails();
            $this->view->organization_details = $organization_details->getOrganizationDetails($id);
        }
    }


}

OrganizationDetails.php //model

<?php

class Application_Model_DbTable_OrganizationDetails extends Zend_Db_Table_Abstract
{

    protected $_name = 'organization_details';

    public function getOrganizationDetails($id)
    {
        $id = (int)$id;
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
        throw new Exception("Could not find row $id");
        }
        return $row->toArray();
    }

    public function addOrganizationDetails($Organization_Name, $Organization_Trading_Name, $Region, $City, $Physical_Address, $Physical_code, $Postal_Address, $Postal_code, $Telephone_Number,$Fax_Number, $Cellular_Number, $Organization_Email, $Organization_Website)
    {
        $data = array(
            'Organization_Name'=> $Organization_Name,
            'Organization_Trading_Name'=> $Organization_Trading_Name,
            'Region'=> $Region,
            'City'=> $City,
            'Physical_Address'=> $Physical_Address,
            'Physical_code'=> $Physical_code,
            'Postal_Address'=> $Postal_Address,
            'Postal_code'=> $Postal_code,
            'Telephone_Number'=> $Telephone_Number,
            'Fax_Number'=> $Fax_Number,
            'Cellular_Number'=> $Cellular_Number,
            'Organization_Email'=> $Organization_Email,
            'Organization_Website'=> $Organization_Website,
        );
        $this->insert($data);
    }

    public function updateOrganizationDetails($id, $Organization_Name, $Organization_Trading_Name, $Region, $City, $Physical_Address, $Physical_code, $Postal_Address, $Postal_code, $Telephone_Number,$Fax_Number, $Cellular_Number, $Organization_Email, $Organization_Website)
    {
        $data = array(
            'Organization_Name'=> $Organization_Name,
            'Organization_Trading_Name'=> $Organization_Trading_Name,
            'Region'=> $Region,
            'City'=> $City,
            'Physical_Address'=> $Physical_Address,
            'Physical_code'=> $Physical_code,
            'Postal_Address'=> $Postal_Address,
            'Postal_code'=> $Postal_code,
            'Telephone_Number'=> $Telephone_Number,
            'Fax_Number'=> $Fax_Number,
            'Cellular_Number'=> $Cellular_Number,
            'Organization_Email'=> $Organization_Email,
            'Organization_Website'=> $Organization_Website,
        );
        $this->update($data, 'id = '. (int)$id);
    }

    public function deleteOrganizationDetails($id)
    {
        $this->delete('id =' . (int)$id);
    }


}

OrganizationDetails.php //forms

<?php
class Application_Form_OrganizationDetails extends Zend_Form
{
    public function init()
    {
        $this->setName('organization_details');
        $id = new Zend_Form_Element_Hidden('id');
        $id->addFilter('Int');

        $Organization_Name = new Zend_Form_Element_Text('Organization_Name');
        $Organization_Name->setLabel('Organization Name')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');

        $Organization_Trading_Name = new Zend_Form_Element_Text('Organization_Trading_Name');
        $Organization_Trading_Name->setLabel('Organization Trading Name')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');

        $Region= new Zend_Form_Element_Select('Region');
        $Region->setAttribs(array('style' => 'width: 150px;'));  
        $Region ->setLabel('Region')
                ->addMultiOptions(array(
                            '--Select One--'=>'--Select One--',
                            'City of Johannesburg'=>'City of Johannesburg',
                            'City of Tswane'=>'City of Tswane',
                            'Ekhurhuleni Metropolitian Municipality'=>'Ekhurhuleni Metropolitian Municipality',
                            'West Rand District Municipality'=>'West Rand District Municipality',
                            'Sedibeng District Municipality '=>'Sedibeng District Municipality',

                        ));


        $City = new Zend_Form_Element_Select('City');
        $City->setAttribs(array('style' => 'width: 144px;'));  
        $City ->setLabel('City')
            ->addMultiOptions(array(
                            'Pretoria'=>'Pretoria',
                            'Gauteng'=>'Gauteng',
                            'Katlego'=>'Katlego',

                        )); 


        $Physical_Address = new Zend_Form_Element_Text('Physical_Address');
        $Physical_Address->setLabel('Physical Address')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');

        $Physical_code = new Zend_Form_Element_Text('Physical_code');
        $Physical_code->setLabel('Physical code')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Postal_Address = new Zend_Form_Element_Text('Postal_Address');
        $Postal_Address->setLabel('Postal Address')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');

        $Postal_code = new Zend_Form_Element_Text('Postal_code');
        $Postal_code->setLabel('Postal code')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Telephone_Number = new Zend_Form_Element_Text('Telephone_Number');
        $Telephone_Number->setLabel('Telephone Number')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Fax_Number = new Zend_Form_Element_Text('Fax_Number');
        $Fax_Number->setLabel('Fax Number')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Cellular_Number = new Zend_Form_Element_Text('Cellular_Number');
        $Cellular_Number->setLabel('Cellular Number')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('Digits');

        $Organization_Email = new Zend_Form_Element_Text('Organization_Email');
        $Organization_Email->setLabel('Organization Email')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('EmailAddress');

        $Organization_Website = new Zend_Form_Element_Text('Organization_Website');
        $Organization_Website->setLabel('Organization Website')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty');




        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setAttrib('id', 'submitbutton');

        $this->addElements(array($id, $Organization_Name, $Organization_Trading_Name, $Region, $City, $Physical_Address, $Physical_code, $Postal_Address, $Postal_code, $Telephone_Number,$Fax_Number, $Cellular_Number, $Organization_Email, $Organization_Website, $submit));
    }
}

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您希望在将第一个表单提交到另一个表单后重定向, 你可以轻松地完成,

 $this->_helper->redirector('action', 'controller');

控制器将具有控制器的名称,操作名称将是您将第二个表单调用的位置。

在提交每个表单后,您可以相应地保存或更新数据。