I have an issue where I cannot seem to get the values to re-populate the zend form. I am using the form a bit different that what I am used to.
I have broken the ‘social security number’ into three fields, the values come out as an array, but I cannot get them to go back in after the operation.
This is my form class:
class Application_Form_Checkssnforexistingreferral extends Zend_Form {
public function init() {
// SSN
$this->addElement('text', 'ss_number', array(
'label' => 'SSN: ',
'required' => True,
));
}
}
Then in my view I use the form like so…
<?php // SSN (first segment)
echo '<p>Social Security Number</p>';
echo $this->formText('ss_number["first"]', '', array(
'size' => 3,
'maxlength' => 3,
'required' => True,
'id' => 'ssn1',
))
?>
<span>-</span>
<?php // SSN (second segment)
echo $this->formText('ss_number["second"]', '', array(
'size' => 2,
'maxlength' => 2,
'required' => True,
'id' => 'ssn2',
))
?>
<span>-</span>
<?php // SSN (third segment)
echo $this->formText('ss_number["third"]', '', array(
'size' => 4,
'maxlength' => 4,
'required' => True,
'id' => 'ssn3',
))
?>
I did it like this so that I may have greater control over the styling and presentation of the form elements, it works quite well on a larger form, although I am having issues populating the fields on that one too.
Here is what I am attempting in the controller…
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$referralsModel = new Application_Service_Findssninreferrals();
$referrals = $referralsModel->findSocialSecurityNumber($formData);
// load the view's parameter 'referral' w/ the object collection
// and 'NULL' the 'first page load' parameter
$this->view->referrals = $referrals;
$first = $formData['ss_number']['"first"'];
$second = $formData['ss_number']['"second"'];
$third = $formData['ss_number']['"third"'];
$form->populate(array('ss_number["first"]' => $first,
'ss_number["second"]' => $second,
'ss_number["third"]' => $third
));
if (empty($referrals)) {
$flashMessenger->addMessage('There is no record found for this SSN, you may create a new referral for this client');
print_r($formData);
$form->populate(array($formData['ss_number']));
$ssn = $first . $second . $third;
$this->view->continueLink = "link to create new referral" . $ssn;
}
} else {
// else populate the form and allow the correction of...
$flashMessenger->addMessage('There was a problem with the number that you entered, please try again...');
$form->populate($formData);
}
}
$this->view->form = $form;
…
this is how one of the elements renders as HTML…
<p>Social Security Number</p>
<input type="text" required="1" maxlength="3" size="3" value="" id="ssn1" name="first" class="idleField">
In the controller, the ‘if(emtpy($referrals))’ section is where I was doing most of the experimenting trying to get it to repopulate the fields. The section above does not work either, I basically attempted to just to a ‘form->populate(array(…’ but with no luck either. I am just not getting anything from the ‘populate’ method…
Try redirecting to the requesting url should take you right back with form populated. Otherwise this probably won’t work, you can’t populate the form without a request (could use ajax) and you don’t have a new request after the post. This one action should probably be at least 2 actions, however
you can try it this way: