0

Good Morning, I work with zend framework (php) and I see in var_dump the value of an option to a select after I send a POST request.

code :

<div class="entry">
<form action="<?php echo $this->escape($this->form->getAction()); ?>" method="<?php echo $this->escape($this->form->getMethod());?>">
 <h2 class="selectVisibility">Seleziona la stagionalità : 
       <select name="cambiaStagionalita">
         <option value="ND"></option>
         <?php foreach ($this->seasons as $season) : 
            $from = new Zend_Date($season['from']);
            $until = new Zend_Date($season['until']);
        ?>
        <option value="<?php echo $season['from'];?>"> Dal <?php echo $from->toString(Zend_Date::DAY_SHORT. " " .Zend_Date::MONTH_NAME. " ".Zend_Date::YEAR);?> al <?php echo $until->toString(Zend_Date::DAY_SHORT. " " .Zend_Date::MONTH_NAME. " ".Zend_Date::YEAR);?></option>
         <?php endforeach; ?>
      </select>
    </h2> <div></div><br/>
    <input type="submit" name="public" value="Cerca"/>

Code php action from controller :

$form = new Application_Form_Hotel_Costs();
$form->createForm2($seasons, $rooms, $mealPlans);
var_dump($form->getValue("cambiaStagionalita"));
//exit(0);

Var_dump returns null .why? The option value is identical to db field.Help !

Nabin Kunwar
  • 1,965
  • 14
  • 29

1 Answers1

0

I assume your Application_Form_Hotel_Costs inherits from Zend_Form and you add some Zend_Form_Elements to the form either in the init() or in the createForm2() method of Application_Form_Hotel_Costs. Then you should populate your form with the data from the request when you receive the POST request. In the controller action that receives the POST I would do it like this:

$form = new Application_Form_Hotel_Costs();
$form->createForm2($seasons, $rooms, $mealPlans);
if ($request->isPost()) {
  $post = $request->getPost ();
  if ($form->isValid($post)) { // populates the form with request data
    var_dump($form->getValue("cambiaStagionalita")); // extract a valid value from the form
  }
}

$this->view->form = $form; //pass the form to the view

The isValid()method of the form validates, filters and populates the form with the data from the request. So after you called that method and it passes, you can extract valid values from the form.

Passing the form to the view can simplyfy you view script to more or less <?php echo $this->form; ?> if you initialized you form correctly. This way you can ommit the part where you render the form yourself and let the default decorator of the form do the job.

The Zend Framework Manual has a good Form Quickstart Tutorial on this whole topic.

p.s. You should not var_dump() values in you controller action. Instead you could pass those values to you view and there display them properly.

marco
  • 252
  • 2
  • 8