我有一个问题,我似乎无法获得重新填充zend表单的值。我使用的形式与我习惯的形式有点不同。
我已将“社会安全号码”分成三个字段,这些值以数组形式出现,但我无法让它们在操作后返回。
这是我的表单类:
class Application_Form_Checkssnforexistingreferral extends Zend_Form {
public function init() {
// SSN
$this->addElement('text', 'ss_number', array(
'label' => 'SSN: ',
'required' => True,
));
}
}
然后在我看来我使用这样的表格......
<?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',
))
?>
我这样做是为了让我可以更好地控制表单元素的样式和表示,它在更大的表单上运行得很好,虽然我也遇到了填充字段上的字段的问题。
以下是我在控制器中尝试的内容......
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;
...
这是其中一个元素呈现为HTML ...
<p>Social Security Number</p>
<input type="text" required="1" maxlength="3" size="3" value="" id="ssn1" name="first" class="idleField">
在控制器中,'if(emtpy($ referrals))部分是我进行大部分试验的地方,试图让它重新填充字段。上面的部分也不起作用,我基本上只是尝试了一个'form-&gt; populate(数组(...'但也没有运气。我只是没有从'populate'方法得到任何东西...... < / p>
答案 0 :(得分:1)
尝试重定向到请求网址应该会在填充表单时向右转。否则这可能不起作用,你不能在没有请求的情况下填充表单(可以使用ajax),并且在帖子之后你没有新的请求。但是,这一个动作应该至少是2个动作 你可以这样试试:
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"'];
if (empty($referrals)) {
$flashMessenger->addMessage('There is no record found for this
SSN, you may create a new referral for this client');
$this->_redirect($this->getRequest()->getRequestUri());
//next line may or may not be needed or help
$form->populate($formData);
$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;