【问题标题】:how do I re-populate this zend form?如何重新填充此 zend 表单?
【发布时间】:2012-02-21 00:44:14
【问题描述】:

我有一个问题,我似乎无法获得重新填充 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(...' 但也没有运气。我只是没有从 'populate' 方法中得到任何东西......

【问题讨论】:

    标签: php zend-framework zend-form


    【解决方案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;
    

    【讨论】:

    • 我的表单实际上是在单击提交按钮时向该 URI 发出请求,如果我这样做,那么它将是第三个请求,而 var $formData 也为空。我认为这与 $formData 作为多维数组有关,因为如果我只是 'echo $form' 并且 $form 是三个输入框,它就可以工作,但它不会按照我需要的方式呈现。不过非常感谢您的回答
    • @rhaag71 您的第一个请求显示表单,第二个请求将表单发布到操作,从该帖子中您可以获得帖子数据。您必须有另一个请求才能重新显示表单并填充字段。就我个人而言,我可能会使用 _forward 到另一个动作来重新显示表单。
    • 好的,我去看看...谢谢!
    • 抱歉耽搁了……我相信你是对的,但我必须做的是,在我的控制器中,是一个 '$form->getElement('elementName')->setValue( '价值');'为了设置大多数元素的值。我认为这是因为我使用表格的方式......这有点不合常规。如果我只是简单地“回显”整个表单,它可以正常工作,但是我使用它的方式(删除许多部分)我不得不手动重新填充表单。幸运的是,获取值对我来说很方便。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-14
    • 2011-03-29
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多