如何在zend中的两个表单页面上清除一组错误

时间:2011-01-17 09:11:33

标签: zend-framework forms error-handling

我有一个单页网站,有两个单独的表单需要回发到同一页面,问题是,如果我提交一个表单,错误检查都在两者上完成,所以显示两者的错误消息。我需要的是,如果表单一是提交,则只显示表单的错误消息,而不是表单二。这可能在zend?

1 个答案:

答案 0 :(得分:0)

对于zend来说这不是问题 - 但是你要解决这个问题! 如果您为表单提供隐藏字段,或者如果您有一个表单唯一的字段ID,您应该能够检查控制器中提交的表单,然后告诉zend您希望它检查哪种表单。像下面这样的东西应该完成这项工作,它将检查ID为unique_form_one_field的字段,显然应该只在表单1上,这可能是一个隐藏字段,例如:

// Get the forms:
$form1 = $this->_helper->formLoader('form_one');
$form2 = $this->_helper->formLoader('form_two');

// Check if there is a POST:
if (!$request->isPost()) 
{
    // It isn't show the forms:
    $this->view->form_one = $form1;
    $this->view->form_two = $form2;
}
else
{
    // It is, get the POST data:
    $post_data = $request->getPost();

    // Check if form one has been submitted:
    if (isset($post_data['unique_form_one_field']))
    {
        // Check if form one is valid:
        if (!$form1->isValid($post_data)) 
        {
            // Its got an error, display the forms again, form one will now be populated with the data and also the error messages:
            $this->view->form_one = $form1;
            $this->view->form_two = $form2;
        }
        else
        {
            // Form one was valid - do what you want to process it:
        }
    }
    else
    {
        // Check if form two is valid:
        if (!$form2->isValid($post_data)) 
        {
            // Its got an error, display the forms again, form two will now be populated with the data and also the error messages:
            $this->view->form_one = $form1;
            $this->view->form_two = $form2;
        }
        else
        {
            // Form two was valid - do what you want to process it:
        }
    }
}