验证和检查表单是否已实际发布的解决方法 [英] Workaround for validation and checking if the form has been actually posted

查看:41
本文介绍了验证和检查表单是否已实际发布的解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的典型表格

        $errors = array();

        if ($this->request->post('submit')) { // <----- I don't like this line
            $post = Validation::factory($this->request->post())
                ->rule('email', 'not_empty')
                ->rule('email', 'email')
                ->rule('password', 'not_empty');

            if ($post->check()) {
                // ok, do something
            }

            $errors = $post->errors(true);
        }

        $this->template->content = View::factory('auth/register')
            ->set('errors', $errors);

您所看到的-我检查是否有一个Submit元素,这意味着我们实际上已经发布了表单,而不仅仅是第一次演出的请求.

As you see - I check if there is a submit element which means that we have actually posted form, not just requested for the first show.

如果我们删除该条件-我们将在首页请求中出现验证错误.有关空电子邮件和密码表单的错误.这实际上是不正确的.

If we remove that condition - we will have validation errors for the first page request. The errors about empty email and password form. Which is actually just incorrect.

那么您如何解决这个问题?

So how do you solve this issue?

推荐答案

这是我要做的,除了以下情况:

This is how I'd do it, except for the condition:

if (Request::POST === $this->request->method())

会更合适.不能跳过" POST检查而不会造成任何后果(例如您所遇到的错误).

would be more suitable. There is no way to "skip" the POST check without having consequences (like the errors in your case).

我们对此主题进行了讨论,5.3可能会添加更多功能.像这样:

We had a discussion on this topic, 5.3 will probably add more features. Something like:

$this->post(function(){
    // do POST-specific stuff 
})
->get(function(){
    // do GET-specific stuff
});

这篇关于验证和检查表单是否已实际发布的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆