Advertisement
Guest User

Sample zend_from + ViewDecorator

a guest
Apr 4th, 2012
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.67 KB | None | 0 0
  1. //form.php
  2. <?php
  3. use Zend_Form as ZendForm,
  4.     Zend_Form_Element_Hidden as HiddenField,
  5.     Zend_Form_Element_Text as InputField,
  6.     Zend_Form_Element_Select as DropdownField,
  7.     Zend_Form_Element_Textarea as TextArea;
  8.  
  9. class CollectionForm extends ZendForm {
  10.     public function init() {
  11.         $card = new \Zend_Form_SubForm;
  12.         $card->setElementsBelongTo('Card');
  13.  
  14.         $fields['CardNumber']       = new InputField('CardNumber',array('required'=>true));
  15.         $fields['ExpirationMonth']  = new InputField('ExpirationMonth',array('required'=>true));
  16.         $fields['ExpirationYear']   = new InputField('ExpirationYear',array('required'=>true));
  17.         $fields['SecurityCode']     = new InputField('SecurityCode',array('required'=>true));
  18.         $fields['NameOnAccount']    = new InputField('NameOnAccount',array('required'=>true));
  19.        
  20.         foreach ($fields as $field) {
  21.             $card->addElement($field);
  22.         }
  23.        
  24.         $address = new \Zend_Form_SubForm;
  25.         $address->setElementsBelongTo('Address',array('required'=>true));
  26.        
  27.         $fields = array();
  28.         $fields['Street']   =  new InputField('Street',array('required'=>true));
  29.         $fields['City']     =  new InputField('City',array('required'=>true));
  30.         $fields['State']    =  new InputField('State',array('required'=>true));
  31.         $fields['Zip']      =  new InputField('Zip',array('required'=>true));
  32.         $fields['Country']  =  new InputField('Country',array('required'=>true));
  33.        
  34.         foreach ($fields as $field) {
  35.             $address->addElement($field);
  36.         }
  37.        
  38.          
  39.         $card->addSubForm($address,'Address');
  40.         $this->addSubForm($card,'Card');
  41.        
  42.        
  43.         $fields = array();
  44.         $fields['entryID']   =  new HiddenField('eventID');
  45.         $fields['registrationID']     =  new HiddenField('registrationID');
  46.        
  47.         foreach ($fields as $field) {
  48.             $this->addElement($field);
  49.         }
  50.     }
  51. }
  52.  
  53. //index.php
  54.  
  55. $form = new CollectionForm;
  56.  
  57. $form->Card->ExpirationMonth = new Zend_Form_Element_Select('ExpirationMonth');
  58. $form->Card->ExpirationMonth->addMultiOptions(array(
  59.     "01"=>"01",
  60.     "02"=>"02",
  61.     "03"=>"03",
  62.     "04"=>"04",
  63.     "05"=>"05",
  64.     "06"=>"06",
  65.     "07"=>"07",
  66.     "08"=>"08",
  67.     "09"=>"09",
  68.     10=>"10",
  69.     11=>"11",
  70.     12=>"12"));
  71.  
  72. $form->Card->ExpirationYear = new Zend_Form_Element_Select('ExpirationYear');
  73. $years = range(date('Y'),date('Y')+10);
  74.  
  75. foreach($years as $year) {
  76.     $options[$year] = $year;
  77. }
  78. $form->Card->ExpirationYear->addMultiOptions($options);
  79.  
  80. $form->Card->Address->State = new Zend_Form_Element_Select('State');
  81. $form->Card->Address->State->addMultiOptions(array(
  82.     "AL" => "Alabama",
  83.     "AK" => "Alaska",
  84.     "AZ" => "Arizona",
  85.     "AR" => "Arkansas",
  86.     "CA" => "California",
  87.     "CO" => "Colorado",
  88.     "CT" => "Connecticut",
  89.     "DE" => "Delaware",
  90.     "DC" => "District of Columbia",
  91.     "FL" => "Florida",
  92.     "GA" => "Georgia",
  93.     "HI" => "Hawaii",
  94.     "ID" => "Idaho",
  95.     "IL" => "Illinois",
  96.     "IN" => "Indiana",
  97.     "IA" => "Iowa",
  98.     "KS" => "Kansas",
  99.     "KY" => "Kentucky",
  100.     "LA" => "Louisiana",
  101.     "ME" => "Maine",
  102.     "MD" => "Maryland",
  103.     "MA" => "Massachusetts",
  104.     "MI" => "Michigan",
  105.     "MN" => "Minnesota",
  106.     "MS" => "Mississippi",
  107.     "MO" => "Missouri",
  108.     "MT" => "Montana",
  109.     "NE" => "Nebraska",
  110.     "NV" => "Nevada",
  111.     "NH" => "New Hampshire",
  112.     "NJ" => "New Jersey",
  113.     "NM" => "New Mexico",
  114.     "NY" => "New York",
  115.     "NC" => "North Carolina",
  116.     "ND" => "North Dakota",
  117.     "OH" => "Ohio",
  118.     "OK" => "Oklahoma",
  119.     "OR" => "Oregon",
  120.     "PA" => "Pennsylvania",
  121.     "RI" => "Rhode Island",
  122.     "SC" => "South Carolina",
  123.     "SD" => "South Dakota",
  124.     "TN" => "Tennessee",
  125.     "TX" => "Texas",
  126.     "UT" => "Utah",
  127.     "VT" => "Vermont",
  128.     "VA" => "Virginia",
  129.     "WA" => "Washington",
  130.     "WV" => "West Virginia",
  131.     "WI" => "Wisconsin",
  132.     "WY" => "Wyoming",
  133.     "NA" => "Not applicable"
  134.     )
  135. );
  136.  
  137. $form->setView($view);
  138. $view->assign('form',$form);
  139. $view->setScriptPath('.');
  140.  
  141. $form->addElement('hidden','token');
  142.  
  143. $view->cost = $_SESSION['cost'];
  144. $view->item = $_SESSION['package'];
  145.  
  146. //form.phtml
  147.  
  148.     <?php
  149.  
  150.         $form = $this->element;
  151.  
  152.         function addErrorsClass($form) {
  153.             foreach($form as $item) {
  154.                 if ($item instanceof Zend_Form) {
  155.                     addErrorsClass($item);
  156.                 }
  157.                 else {
  158.                     if ($item->hasErrors()) {
  159.                         $item->setAttrib('class','error');
  160.                     }
  161.                 }
  162.                
  163.             }
  164.         }
  165.        
  166.         addErrorsClass($form);
  167.         $view = $form->getView();
  168.         function addView($form) {
  169.             global $view;
  170.             foreach($form as $item) {
  171.                 if ($item instanceof Zend_Form) {
  172.                     $item->setView($view);
  173.                     addView($item);
  174.                 }
  175.                 else {
  176.                     $item->setView($view);
  177.                 }
  178.                
  179.             }
  180.         }
  181.        
  182.         addView($form);
  183.     ?>
  184.    
  185.  
  186. <div class="container">
  187.  
  188. <form id="checkoutForm" class="" action="" method="post">
  189.     <?php echo $form->token->renderViewHelper()?>
  190.     <div class="ib-cont row r">  
  191.         <label class="ib">Name on Card</label>
  192.         <div class="ib input-wrapper">
  193.             <?php echo $form->Card->NameOnAccount->renderViewHelper()?>
  194.         </div>
  195.     </div>
  196.    
  197.     <div class="ib-cont row r">  
  198.         <label class="ib">Credit Card No</label>
  199.         <div class="ib input-wrapper">
  200.             <?php echo $form->Card->CardNumber->renderViewHelper()?>
  201.         </div>
  202.     </div>
  203.    
  204.     <div class="ib-cont row">
  205.         <label class="ib">Expiration</label>
  206.         <div class="ib input-wrapper month">
  207.             <?php echo $form->Card->ExpirationMonth->renderViewHelper()?>
  208.         </div><!--
  209.         --><div class="ib input-wrapper year">
  210.             <?php echo $form->Card->ExpirationYear->renderViewHelper()?>
  211.         </div>             
  212.     </div>
  213.        
  214.     <div class="ib-cont row">
  215.         <label class="ib">CVV</label>
  216.         <div class="ib input-wrapper">
  217.             <?php echo $form->Card->SecurityCode->renderViewHelper()?>
  218.         </div>
  219.     </div>
  220.        
  221.     <div class="ib-cont row">
  222.         <label class="ib">Address</label>
  223.         <?php echo $form->Card->Address->Street->setBelongsTo('Card[Address]')->renderViewHelper()?>
  224.      </div>
  225.      
  226.        
  227.      <div class="ib-cont row">
  228.             <label class="ib">City</label>
  229.             <div class="ib input-wrapper">
  230.                 <?php echo $form->Card->Address->City->setBelongsTo('Card[Address]')->renderViewHelper()?>
  231.             </div>
  232.         </div>
  233.        
  234.         <div class="ib-cont row state">
  235.             <label class="ib">State</label>
  236.             <div class="ib input-wrapper">             
  237.                 <?php echo $form->Card->Address->State->setBelongsTo('Card[Address]')->renderViewHelper()?>
  238.             </div>
  239.         </div>
  240.        
  241.         <div class="ib-cont row">
  242.             <label class="ib">Country</label>
  243.             <div class="ib input-wrapper">             
  244.                 <?php echo $form->Card->Address->Country->setBelongsTo('Card[Address]')->renderViewHelper()?>
  245.             </div>
  246.         </div>
  247.        
  248.         <div class="ib-cont row">
  249.             <label class="ib">Zip</label>
  250.             <div class="ib input-wrapper">             
  251.                 <?php echo $form->Card->Address->Zip->setBelongsTo('Card[Address]')->renderViewHelper()?>
  252.             </div>
  253.         </div>
  254.        
  255.         <div class="ib-cont row">
  256.             <div class="ib secure">
  257.                 <ins class="ib sprite lock"></ins>
  258.                 <span>Pay using our secure server</span>
  259.             </div>
  260.             <div class="ib input-wrapper submit">              
  261.             <input id="Button1" type="submit" name="Submit" value="Checkout" class="button" />
  262.             </div>
  263.         </div>
  264. </form>
  265.  
  266. </div>
  267.  
  268. //index.phtml
  269.  
  270. <?php
  271. echo $this->form->setDecorators( array(  
  272.     'PrepareElements',
  273.     array('ViewScript', array(
  274.         'viewScript' => 'form.phtml'))))->render($this);
  275. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement