如何在模块中使用ExpressionEngine表单验证类来重新填充模板中的表单?

时间:2011-03-15 20:55:03

标签: php validation templates forms expressionengine

是否可以在硬编码到模板中的表单上显示错误并重新填充字段?到目前为止,我只是想出了如何在模块的视图中显示错误,而不是模板。

基于ExpressionEngine的逻辑,我猜我需要通过模块中的标签以某种方式使验证错误可见,或者甚至从模块生成整个表单,但我不确定如何最好地接近此

这就是我现在所拥有的。

function submit_form()
{        
    $this->EE->load->helper('form');
    $this->EE->load->library('form_validation');

    $this->EE->form_validation->set_rules('first_name', 'First Name', 'required');
    $this->EE->form_validation->set_rules('last_name', 'Last Name', 'required');
    $this->EE->form_validation->set_rules('address', 'Address', 'required');
    $this->EE->form_validation->set_rules('city', 'City', 'required');
    $this->EE->form_validation->set_rules('province', 'Province', 'required');

    if ($this->EE->form_validation->run() == FALSE)
    {
        return $this->EE->load->view('form_errors');
    }
    else
    {
        // success
    }
} 

对于测试,视图只包含:

echo validation_errors(); 

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:10)

很好的问题,而且花了很长时间才找出最佳解决方法。

CodeIgniter Form Validation library非常棒,但只能与正确的视图和控制器一起使用,因此在开发前端标记时它不能立即使用。

通常,提交前端表单的首选方法是在upd.addon.php文件中注册一个“Action”(我猜你已经为你的submit_form()函数做了这个)。然后为此分配一个数字,您可以使用网址/index.php?ACT=37或类似内容发布。这是一个很好的系统,因为这意味着我们知道表单提交来自我们的模块。但是,对于输入表单,这是一个障碍,因为这意味着我们无法重新填充输入字段。因此,您需要配置输入表单以回发到当前URL,并等待模板引擎在处理表单提交之前尝试呈现您的标记。

实现这一目标的最简单,最直观的方法是使用$this->EE->output->show_user_error(FALSE, array_of_errors)。实际上,您可以从操作或模块代码中使用它。它显示了我们都知道并讨厌的标准灰色EE消息页面。

通过这种介绍,您可能知道解决方案不会那么简单,对吧?这是标记函数的框架,它实现了内联错误检查:

function my_form()
{
    // load default tag variables
    $tag_vars = array();
    $tag_vars[0] = array(
        'first_name' => '',
        'error:first_name' => '',
        'last_name' => '',
        'error:last_name' => ''
    );

    // handle a form submission
    if ($this->EE->input->post('my_form_hidden') == '1'))
    {
        // load POST data into tag
        $tag_vars[0]['first_name'] = $this->EE->input->post('first_name', TRUE);
        $tag_vars[0]['last_name'] = $this->EE->input->post('last_name', TRUE);

        // use CI validation library to check submission
        $this->EE->load->helper('form');
        $this->EE->load->library('form_validation');
        $this->EE->form_validation->set_rules('first_name', 'lang:first_name', 'required');
        $this->EE->form_validation->set_rules('last_name', 'lang:first_name', 'required');

        $valid_form = $this->EE->form_validation->run();
        if ($valid_form)
        {
            // probably save something to database, then redirect
        }
        else
        {
            $form_errors = array();
            foreach (array('first_name', 'last_name') as $field_name)
            {
                $field_error = form_error($field_name);
                if ($field_error)
                {
                    $form_errors[] = $field_error;
                    $tag_vars[0]['error:'.$field_name] = $field_error;
                }
            }

            if ($this->EE->TMPL->fetch_param('error_handling') != 'inline')
            {
                // show default EE error page
                return $this->EE->output->show_user_error(FALSE, $form_errors);
            }
        }
    }

    // parse and output tagdata
    $out = $this->EE->functions->form_declaration(array(
        'action' => $this->EE->functions->fetch_current_uri(),
        'hidden_fields' => array('my_form_hidden')));
    $out .= $this->EE->TMPL->parse_variables($tagdata, $tag_vars);
    return $out.'</form>';
}

这样,设计人员可以在标签中指定error_handling="inline",如果他们想要内联错误,否则他们只会被重定向到标准错误表单。如果他们确实请求内联错误处理,他们只需要确保他们的输入如下所示:

<input type="text" name="first_name" value="{first_name}" />
{error:first_name}

请注意我们随表单提交的隐藏字段,这样我们就可以确保只处理表单的提交,而不是页面上的任何其他表单,例如登录表单或其他内容。如果您在页面上有多个表单实例(比如在通道条目循环或其他内容中),则需要实现一些技巧以确保您只处理已提交的表单实例,例如通过提交entry_id也是一个隐藏字段。

很高兴得到所有记录,希望这对其他EE开发人员也有用!