Guest

Jeremy Kendall

By: a guest on Jan 21st, 2009  |  syntax: PHP  |  size: 1.48 KB  |  hits: 2,579  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. <?php
  2.  
  3. /**
  4.  * FormsController
  5.  *
  6.  * @author Jeremy Kendall <jeremy@jeremykendall.net>
  7.  */
  8.  
  9. require_once 'Zend/Controller/Action.php';
  10.  
  11. class FormsController extends Zend_Controller_Action {
  12.  
  13.   /**
  14.    * The default action - show the home page
  15.    */
  16.   public function indexAction() {
  17.     // TODO Auto-generated FormsController::indexAction() default action
  18.   }
  19.  
  20.   /**
  21.    * Shows the dynamic form demonstration page
  22.    */
  23.   public function dynamicFormElementsAction() {
  24.        
  25.     $form = new Code_Form_Dynamic();
  26.    
  27.     // Form has not been submitted - pass to view and return
  28.     if (!$this->getRequest()->isPost()) {
  29.       $this->view->form = $form;
  30.       return;
  31.     }
  32.  
  33.      // Form has been submitted - run data through preValidation()
  34.     $form->preValidation($_POST);
  35.    
  36.      // If the form doesn't validate, pass to view and return
  37.     if (!$form->isValid($_POST)) {
  38.       $this->view->form = $form;
  39.       return;
  40.     }
  41.    
  42.      // Form is valid
  43.     $this->view->form = $form;
  44.   }
  45.  
  46.   /**
  47.    * Ajax action that returns the dynamic form field
  48.    */
  49.   public function newfieldAction() {
  50.    
  51.     $ajaxContext = $this->_helper->getHelper('AjaxContext');
  52.     $ajaxContext->addActionContext('newfield', 'html')->initContext();
  53.    
  54.     $id = $this->_getParam('id', null);
  55.    
  56.     $element = new Zend_Form_Element_Text("newName$id");
  57.     $element->setRequired(true)->setLabel('Name');
  58.    
  59.     $this->view->field = $element->__toString();
  60.   }
  61. }