# Zend form parameters - how to change after instanciated

I have a form that I need to resubmit to the action if a checkbox is TRUE. This is to facilitate chained select boxes with javascript turned off. Ideally I would instanciate the form object at the end only, just before sending it to the view, but as you can see I need it beforehand.

The form takes parameters (an array of lists used to populate the select boxes), and these parameters are not yet calculated at the point where I have to first instanciate the form object. Is there a way to instantiate the form and add the parameters later, thus avoiding having to re-instanciate the form at the end when I have the definitive lists?

Can anyone see a better way of doing this?

public function editcontactdetailsAction() {

$auth = Zend_Auth::getInstance();$id = $auth->getidentity();$user = $this->em->getRepository('Entities\User') ->findOneByid($id);
$town =$user->getTown();

// initialize list values as null
$townlist = array();$provincelist= array();
$regionlist=array();$countrylist=array();
//initialize ids as 0 so defaults can be set
$townid=0;$provinceid=0;
$regionid=0;$countryid=0;
//initialize $data variable used to set defaults later$data=null;

// HAS town - get corresponding lists for dropdowns
if (!is_null($town)) {$townid = $town->getId(); //get the province$province = $town->getProvince();$provinceid = $province->getId(); //get the region$region = $province->getRegion();$regionid = $region->getId(); //get the country$country = $region->getCountry();$countryid = $country->getId();$townlist = $this->em->getRepository('Entities\Town')->findActiveTowns($provinceid);
$provincelist =$this->em->getRepository('Entities\Province')->findActiveProvinces($regionid);$regionlist = $this->em->getRepository('Entities\Region')->findActiveRegions($countryid);
}

// even if user NO town, populate the countries list
$countrylist =$this->em->getRepository('Entities\Country')->findActiveCountries();

$lists = array($townlist, $provincelist,$regionlist, $countrylist);$form = new Application_Model_FormContactDetails($lists); // //--------------------------------------------------------------- // Has the form been submitted? if ($this->getRequest()->isPost()) {
//if we dont include the $validformline, the form is not populated with the post data$validform =$form->isValid($this->_request->getPost());
//Is this a request to refresh the regional structure and not a real submit
if($form->getValue('refreshregionalstructure')==1){ //find out up to what the level the user has selected, starting from the bottom$formprovincevalue=$form->getValue('province'); if(!$formprovincevalue==0){
$townlist =$this->em->getRepository('Entities\Town')->findActiveTowns($formprovincevalue); } } else{ // If the form data is valid, process it if ($form->isValid($this->_request->getPost())) { //save all to db try { // Save the user to the database // Set the flash message$this->_helper->flashMessenger->addMessage(array('success' =>
_('The contact details were updated')));

$this->_redirect('/account/index'); } catch (Exception$e) {
$this->em->close();$this->view->errors = array(array(_('There was a problem editing your contact details.')));
}
} else {
$this->view->errors =$form->getErrors();
}
}
} else { //form has not been submitted yet
// populate form with user record
$data = array('firstname' =>$user->getFirstname(), 'middlename' => $user->getMiddlename(), 'surname' =>$user->getSurname(), 'address1' => $user->getAddress1(), 'address2' =>$user->getAddress2(),'town'=> $townid, 'province'=>$provinceid,'region'=> $regionid,'country'=>$countryid);

}
$lists = array($townlist, $provincelist,$regionlist, $countrylist);$form = new Application_Model_FormContactDetails($lists); //if we dont include the$validform line, the form is not populated with the post data
$validform =$form->isValid($this->_request->getPost()); if (!is_null($data))$form->setDefaults($data);
$this->view->form =$form;
}

-

## migrated from stackoverflow.comJan 22 '12 at 15:20

This question came from our site for professional and enthusiast programmers.

I did it like this in the end, I'm not sure if it's the best way and I would welcome comments.

public function editcontactdetailsAction() {

$auth = Zend_Auth::getInstance();$id = $auth->getidentity();$user = $this->em->getRepository('Entities\User') ->findOneByid($id);
$town =$user->getTown();

// initialize list values as null
$townlist = array();$provincelist = array();
$regionlist = array();$countrylist = array();
//initialize ids as 0 so defaults can be set
$townid = 0;$provinceid = 0;
$regionid = 0;$countryid = 0;
//initialize $data variable used to set defaults later$data = null;

// HAS town - get corresponding lists for dropdowns
if (!is_null($town)) {$townid = $town->getId(); //get the province$province = $town->getProvince();$provinceid = $province->getId(); //get the region$region = $province->getRegion();$regionid = $region->getId(); //get the country$country = $region->getCountry();$countryid = $country->getId();$townlist = $this->em->getRepository('Entities\Town')->findActiveTowns($provinceid);
$provincelist =$this->em->getRepository('Entities\Province')->findActiveProvinces($regionid);$regionlist = $this->em->getRepository('Entities\Region')->findActiveRegions($countryid);
}

// even if user NO town, populate the countries list
$countrylist =$this->em->getRepository('Entities\Country')->findActiveCountries();

//---------------------------------------------------------------
// Has the form been submitted?
if ($this->getRequest()->isPost()) { if ($this->getRequest()->getPost('refreshregionalstructure') == 1) {
//find out up to what the level the user has selected, starting from the bottom

$formprovincevalue =$this->getRequest()->getPost('province');
if (!$formprovincevalue == 0) {$townlist = $this->em->getRepository('Entities\Town')->findActiveTowns($formprovincevalue);
}

$lists = array($townlist, $provincelist,$regionlist, $countrylist);$form = new Application_Model_FormContactDetails($lists); //if we dont include the$validform line, the form is not populated with the post data
$validform =$form->isValid($this->_request->getPost()); } else {$lists = array($townlist,$provincelist, $regionlist,$countrylist);
$form = new Application_Model_FormContactDetails($lists);
// If the form data is valid, process it
if ($form->isValid($this->_request->getPost())) {

//save all to db
try {

// Save the user to the database
// Set the flash message
$this->_helper->flashMessenger->addMessage(array('success' => _('The contact details were updated'))); // Redirect the user to the home page$this->_redirect('/account/index');
} catch (Exception $e) {$this->em->close();
$this->view->errors = array(array(_('There was a problem editing your contact details.'))); } } else {$this->view->errors = $form->getErrors(); } } } else { //form has not been submitted yet // populate form with user record$data = array('firstname' => $user->getFirstname(), 'middlename' =>$user->getMiddlename(),
'surname' => $user->getSurname(), 'address1' =>$user->getAddress1(),
'address2' => $user->getAddress2(), 'town' =>$townid,
'province' => $provinceid, 'region' =>$regionid, 'country' => $countryid);$lists = array($townlist,$provincelist, $regionlist,$countrylist);

$form = new Application_Model_FormContactDetails($lists);
//if we dont include the $validform line, the form is not populated with the post data$validform = $form->isValid($this->_request->getPost());
}

if (!is_null($data))$form->setDefaults($data);$this->view->form = \$form;
}

-