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

标签 zend-framework forms error-handling

我有一个一页的网站,其中有两个单独的表单需要重新发布到同一页面,问题是,如果我提交一个表单,则在两个表单上都进行了错误检查,因此显示了两个表单的错误消息。我需要的是,如果提交表单一,则仅出现表单一的错误消息,而不是表单二。 zend有可能吗?

最佳答案

zend要做的不是问题-但这是您要解决的问题!
如果为表单提供一个隐藏字段,或者具有一个表单唯一的字段ID,则应该能够检查 Controller 中已提交的表单,然后告诉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:
        }
    }
}

关于zend-framework - 如何清除zend中两个表单页面上的一组错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4711520/

相关文章:

mysql - 如何使用 Zend Adapter 从数据库中检索信息

php - 从使用 php 构建的 Web 服务中获取空响应

php - 使用 MySQL 将多个表单的数据插入到多个表中

database - 如何从reactjs发出Http请求?

php - 将 Facebook Connect 登录/注册与 Zend Framework 应用程序集成

vba - 从旋转按钮更新表单组件可见性的更好方法?

regex - HTML5 验证模式例如 : 13-124132424

swift - 在 Swift 中转发错误

c# - ASP.NET MVC2 中正确的错误处理

powershell - 错误处理并最小化脚本输出给最终用户