【问题标题】:How to use ExpressionEngine form validation class in module to repopulate form in template?如何在模块中使用 ExpressionEngine 表单验证类在模板中重新填充表单?
【发布时间】:2011-07-16 03:11:16
【问题描述】:

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

基于 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(); 

谁能帮忙?

【问题讨论】:

    标签: php validation templates forms expressionengine


    【解决方案1】:

    很好的问题,我花了很长时间才找到最好的解决方法。

    CodeIgniter Form Validation library 很棒,但只能与适当的视图和控制器一起使用,因此在开发前端标签时它不能开箱即用。

    通常,提交前端表单的首选方法是在您的 upd.addon.php 文件中注册一个“操作”(我猜您已经为您的submit_form() 函数完成了此操作)。然后会为其分配一个编号,您可以使用 url /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}
    

    注意我们与表单一起提交的隐藏字段,这使我们能够确保我们只处理 this 表单的提交,而不是页面上的任何其他表单,例如登录表单或其他东西.如果您在一个页面上有多个表单实例(例如在通道条目循环内或其他地方),您将需要实施一些技巧以确保您只处理提交的表单实例,例如通过提交entry_id 也是隐藏字段。

    很高兴将所有内容记录在案,希望这对其他 EE 开发人员也有用!

    【讨论】:

    • 非常感谢,阿德里安!有点麻烦,但现在我知道了。如果我也有一些实时 JavaScript 验证(大多数访问者会看到),似乎传递到一个显示错误的视图并带有一个用于更正的后退按钮不会有什么大不了的。
    • 是的,如果您只是为自己的网站开发并且知道前端会有 JS 验证,那么这实际上是一个非常聪明的解决方案。如果您正在开发商业模块并希望设计人员具有最大的灵活性,我的解决方案可能会更有帮助。
    • 谢谢!我想出了如何通过操作来处理表单,但验证让我失望了。
    猜你喜欢
    • 2010-11-10
    • 2012-05-05
    • 2013-06-11
    • 2013-02-28
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    相关资源
    最近更新 更多