Example #1
0
 /**
  * Get an Add/Edit form for the object.
  *
  * @param string $target Post target for form submission
  */
 public function getAddEditForm($target = '/admin/Cart')
 {
     $form = new Form('CartShippingRate_addedit', 'post', $target);
     $form->setConstants(array('section' => 'shipping'));
     $form->addElement('hidden', 'section');
     $form->setConstants(array('action' => 'addedit'));
     $form->addElement('hidden', 'action');
     if (!is_null($this->getId())) {
         $form->setConstants(array('cartshippingrate_id' => $this->getId()));
         $form->addElement('hidden', 'cartshippingrate_id');
         $defaultValues['cartshippingrate_state'] = $this->getState();
         $defaultValues['cartshippingrate_country'] = $this->getCountry();
         $defaultValues['cartshippingrate_cost'] = $this->getCost();
         $form->setDefaults($defaultValues);
     }
     $countrySelect = array('onchange' => 'selectCountry();', 'id' => 'cartshippingrate_country');
     $form->addElement('select', 'cartshippingrate_country', 'Country', Form::getCountryArray(), $countrySelect);
     $form->addElement('select', 'cartshippingrate_state', 'Province/State', Form::getStatesArray("Canada"));
     $form->addElement('text', 'cartshippingrate_cost', 'Shipping Cost');
     $form->addElement('submit', 'cartshippingrate_submit', 'Submit');
     if ($form->validate() && $form->isSubmitted()) {
         $selectedCountry = $form->exportValue('cartshippingrate_country');
         //Only if the selected country is Canada, insert the selected state.
         if (CartShippingRate::hasState($selectedCountry)) {
             $selectedState = $form->exportValue('cartshippingrate_state');
         } else {
             $selectedState = 0;
         }
         $this->setCountry($selectedCountry);
         $this->setState($selectedState);
         $this->setCost($form->exportValue('cartshippingrate_cost'));
         $this->save();
     }
     return $form;
 }