pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

jeremykendall private pastebin - collaborative debugging tool What's a private pastebin?


Posted by Jeremy Kendall on Wed 21 Jan 12:30
report abuse | download | new post

  1. <?php
  2.  
  3. /**
  4.  * @see Zend_Form
  5.  */
  6. require_once 'Zend/Form.php';
  7.  
  8. /**
  9.  * Proof of concept: dynamically adding elements to a Zend_Form
  10.  */
  11. class Code_Form_Dynamic extends Zend_Form {
  12.        
  13.   public function init() {
  14.        
  15.     $this->addElement('hidden', 'id', array(
  16.       'value' => 1
  17.     ));
  18.    
  19.     $this->addElement('text', 'name', array(
  20.       'required' => true,
  21.       'label'    => 'Name',
  22.       'order'    => 2,
  23.     ));
  24.    
  25.     $this->addElement('button', 'addElement', array(
  26.       'label' => 'Add',
  27.       'order' => 91
  28.     ));
  29.    
  30.     $this->addElement('button', 'removeElement', array(
  31.       'label' => 'Remove',
  32.       'order' => 92
  33.     ));
  34.    
  35.     // Submit
  36.     $this->addElement('submit', 'submit', array(
  37.       'label' => 'Submit',
  38.       'order' => 93
  39.     ));
  40.   }
  41.  
  42.   /**
  43.    * After post, pre validation hook
  44.    *
  45.    * Finds all fields where name includes 'newName' and uses addNewField to add
  46.    * them to the form object
  47.    *
  48.    * @param array $data $_GET or $_POST
  49.    */
  50.   public function preValidation(array $data) {
  51.  
  52.     // array_filter callback
  53.     function findFields($field) {
  54.       // return field names that include 'newName'
  55.       if (strpos($field, 'newName') !== false) {
  56.         return $field;
  57.       }
  58.     }
  59.    
  60.     // Search $data for dynamically added fields using findFields callback
  61.     $newFields = array_filter(array_keys($data), 'findFields');
  62.    
  63.     foreach ($newFields as $fieldName) {
  64.       // strip the id number off of the field name and use it to set new order
  65.       $order = ltrim($fieldName, 'newName') + 2;
  66.       $this->addNewField($fieldName, $data[$fieldName], $order);
  67.     }
  68.   }
  69.  
  70.   /**
  71.    * Adds new fields to form
  72.    *
  73.    * @param string $name
  74.    * @param string $value
  75.    * @param int    $order
  76.    */
  77.   public function addNewField($name, $value, $order) {
  78.    
  79.     $this->addElement('text', $name, array(
  80.       'required'       => true,
  81.       'label'          => 'Name',
  82.       'value'          => $value,
  83.       'order'          => $order
  84.     ));
  85.   }
  86. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post