2012-02-21 71 views
1

我有一个问题,我似乎无法得到重新填充zend表单的值。我使用的形式与我习惯的有所不同。如何重新填充此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-> populate(array(...'但是没有运气。我只是没有从'填充'方法得到任何东西...

回答

1

尝试重定向到请求的url应该带你回到表单填充。否则这可能不会工作,你不能填充表单没有请求(可以使用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; 
+0

我的形式实际上是发出请求到URI时,点击提交按钮,如果我这样做像这样,那么这将是第三个要求,离开了VA r $ formData也是空的。我认为它与$ formData是一个多维数组有关,因为它可以工作,如果我只是'echo $ form'而$ form是三个输入框,它不会呈现我需要的方式。但是,非常感谢您的回答 – rhaag71 2012-02-21 07:00:43

+0

@ rhaag71您的第一个请求显示表单,第二个表单将表单发布到操作中,从这篇文章中您可以获取发布数据。您必须再次请求重新显示表单并填充字段。就我个人而言,我可能会使用_forward来执行另一个操作来重新显示表单。 – RockyFord 2012-02-22 03:14:11

+0

好的,我会检查一下......谢谢! – rhaag71 2012-02-22 07:19:39