Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I simplify Form Processing in 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).

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.

like image 211
Alix Axel Avatar asked Apr 11 '09 15:04

Alix Axel


People also ask

What is basic form of processing in PHP?

< br > < input type = "submit" value="Submit”/> </ form >

How forms are formed in PHP explain it using a suitable example?

PHP Form Handling We can create and use forms in PHP. To get form data, we need to use PHP superglobals $_GET and $_POST. The form request may be get or post. To retrieve data from get request, we need to use $_GET, for post request $_POST.

How can I access form data in PHP?

$_POST['firstname']: The form data is stored in the $_POST['name as key'] variable array by PHP since it is submitted through the POST method, and the element name attribute value – firstname (name=”firstname”) is used to access its form field data.

What is the importance of form handling in PHP?

Form handling is the very basic and important feature of PHP. For form creation, we should use HTML. Using forms we can accept data from user and then we can handle the data using PHP. Data can be saved in any database server like MySql.


3 Answers

IMHO, the attempt to treat a form as a single concept is a failure. If you have any kind of layered architecture to your application, forms are likely to cut across these. Forms have application logic (controller layer), they have a visual representation (view layer), they have state (application layer model), and in the end they usually invoke some kind of transactional script (model layer).

I think you're much better off dropping the idea of a "form" as an entity and instead focus on the three parts (input processing, rendering and model layer) as completely separate matters, that just might (or might not) happen to be closely related to each other. This is some times referred to as the MVC pattern, although the term is so strongly loaded by now that it could mean a lot of things.

like image 134
troelskn Avatar answered Oct 01 '22 10:10

troelskn


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.

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.

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.

like image 20
zalew Avatar answered Oct 02 '22 10:10

zalew


I know it's something you excluded, and me too was like you up until a year ago, when I forced myself to learn something obscure like Qcodo (php framework), and lo and behold, I can't do anything without it noways. It's simply wonderful to take off lots of always repeating burden off your shoulders. Why Qcodo? At that time I wanted to learn the most advanced one since I'm already learning, so I looked for the most broad feature set, which Qcodo seemed to offer. Today I don't know which one is the hottest, but for me Qcodo is still fulfilling all my needs.

like image 30
Mark Avatar answered Oct 02 '22 10:10

Mark