如何简化PHP中的表单处理? [英] How do I simplify Form Processing in PHP?

查看:66
本文介绍了如何简化PHP中的表单处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从事PHP开发已经有一段时间了,但是直到今天,我还没有找到一种简单的处理方法(即规范化,清理,验证,填充和显示表单以及相应的字段错误).

I'm been a PHP developer for quite some time now but until today I've not found a simple way to process (aka normalize, sanitize, validate, populate and display forms and it's respective field errors).

我知道当今大多数PHP框架使这项工作变得更容易,但是我不愿意将我的所有代码都移植到这些框架之一,而且我不太了解在Django中如何实现表单验证,实例(我知道,它是Python,但我真的很喜欢他们的方法),所以我虽然最好的方法是让我在这里发布处理简单表单的方式,也许你们可以为我指明最佳的方向.

I know that most of the PHP frameworks nowadays make this job easier but somehow I don't feel like porting all my code to one of these frameworks, and I can't quite understand how the form validation is implemented in Django for instance (I know, it's Python but I really like their approach), so I though the best way would for me to post here the way I process a simple form and maybe you guys can point me in the best direction.

<?php

// sample controller
class _sample extends framework
{
    // sample action
    function contact()
    {
        if ($this->Is->Post() === true)
        {
            $errors = array();

            if ($this->Is->Set($_POST['name']) === false)
            {
                $errors['name'] = 'Please fill in your name.';
            }

            if (($this->Is->Email($_POST['email']) === false) || ($this->Is->Set($_POST['email']) === false))
            {
                $errors['email'] = 'Please fill in your email address.';
            }

            if (($this->Is->Phone($_POST['contact']) === false) && ($this->Is->Mobile($_POST['contact']) === false))
            {
                $errors['contact'] = 'Please fill in your phone (or cell phone) number.';
            }

            if ($this->Is->Set($_POST['message']) === false)
            {
                $errors['message'] = 'Please type a message';
            }

            // no errors, it's valid!
            if (empty($errors) === true)
            {
                // do stuff and redirect to "success" / "thank you" page
            }

            // load the form view, and let it display the errors
            // automatically prefill fields with $_POST values
            else
            {
                $this->View('contact_form', $errors);
            }
        }

        // load the form view for the first time
        else
        {
            $this->View('contact_form');
        }
    }
}

?>

如您所见,这应该是一个简单的联系表,但是它使我无法抽身来验证它,我一直在研究某些设计模式(观察员,工厂),但是我不确定是否和我应该以哪种方式实施它们.

As you can see, this is supposed to be a simple contact form however it takes the life out of me to validate it, I have been looking into some design patterns (Observer, Factory) but I do not feel confident if and in what way I should implement them.

推荐答案

您可以为所有表单创建一个抽象基类,为字段类型创建一个类,并为验证各种类型(validateString,validateHtml, validateEmail,validateNumber,日期等,仅是方法..).定义表单后,您将定义它将使用的字段对象,因此Form-> validate()方法将调用Field-> validate()并返回过滤后的值或错误消息.指定字段的默认错误消息,但是在定义表单类中的字段时可以选择覆盖它.

You could make an abstract base class for all your forms, classes for fieldtypes, and a static class just for validating the values of various types (validateString, validateHtml, validateEmail, validateNumber, date, etc, just the methods..). Defining your form, you would define what field objects would it use, so the Form->validate() method would invoke the Field->validate() and return the filtered value or error message. Specify default error messages for the fields, but give an option to override it when defining fields in your form class.

哦,留下$ _POST东西.请阅读一次,将其传递给表单验证一次,然后处理过滤后的字段值.

Oh, and leave that $_POST thing. Read the post once, pass it once to the form validation and then work on filtered field values.

另一件事是,有多种方法可以根据您的需求和应用程序的体系结构来实现表单验证,当您采用各种方法进行应用程序设计时,很难形成通用的表单验证器.选择一种工作方式并坚持下去(不管它是现成的框架还是您自己的代码),或者您编写的任何超级复制形式验证,在以后的项目中都没有意义.

Another thing is there are various ways to achieve form validation depending on your needs and the architecture of your applications, it can be hard to make an all-purpose form validator when you have various approaches to your application design. Choose a way of doing your work and stick to it (regardless if it's a ready to go framework or your own code), or whatever super-duper-form-validation you write, it will make no sense in latter projects.

还有一个:像Django?好的!因此,开始在Django中进行Python编程,您将真正改变思考如何完成工作的方式.

One more: like Django? Good! So start programming Python in Django, you'll really change the way of thinking how to get your job done.

这篇关于如何简化PHP中的表单处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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