2012-02-21 9 views
1

私はzendフォームを再設定する値を取得できないような問題があります。私は、私が慣れているものとは少し違った形を使っています。このzendフォームはどのように再作成しますか?

私は「社会保障番号」を3つのフィールドに分割しました。値は配列として出てきますが、操作後に戻すことはできません。

これは私のフォームクラスである:

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(...')でも運がないだけで、 'populate'メソッドから何も得られません...

答えて

1

リクエストしたURLにリダイレクトすると、フォームが入力された状態ですぐに戻る必要があります。そうでないと、おそらく動作しませんし、要求なしでフォームに入力できず(ajaxを使用することもできます)、 。ポストこの1つのアクションは、おそらく、少なくとも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に要求を行っていますこれのように3番目のリクエストになります。 r $ formDataも空です。私はそれが私がちょうど 'エコー$フォーム'と$フォームは3つの入力ボックスが、私はそれを必要とする方法をレンダリングされない場合に動作するので、多面的な配列である$ formDataともっと関係があると思います。しかし、お返事ありがとうございます – rhaag71

+0

@ rhaag71最初のリクエストはフォームを表示し、2番目のフォームはアクションのフォームを投稿し、この投稿から投稿データを取得します。フォームを再表示してフィールドに入力するようにもう一度要求する必要があります。個人的には、フォームを再表示するために別のアクションに_forwardを使用しています。 – RockyFord

+0

これを確認します...ありがとう! – rhaag71

関連する問題