HTML-Forms are simple to write – but to do it in a good way and to make them comfortable to use is not an easy task.
There are a few things you will have to do again and again:
Because we did not want to fight anymore, we are introducing today:
depage-forms – html5-forms made easy!
depage-forms is PHP library for HTML form generation with focus on usability for developers and users.
It is part of the upcoming version of depage-cms, but it also works as a standalone library. By abstracting HTML, browser flaws (duplicate form submissions) and form validation, it provides a comfortable way to obtain reliable and validated data from users.
We are (by far) not the first trying to solve these problems. For PHP there are Zend-Forms and Phorms for example. But I think we found a rather unique approach to solve these problem. So, how does it work?
Get composer at http://getcomposer.org and then just add this to your composer.json.
composer require depage/htmlform
to install the current version of depage-htmlform into your vendor dir.
First we load the Library and initialize an instance of the htmlforms-class. Almost every action goes through the htmlforms-class. Besides fieldsets and steps (later more about this) we only talk to instances of the htmlform directly.
After that, we add our inputs and other formfields to our form.
You can do this by calling the '"add" + element type' method. The first parameter is the name of the element. It has to be unique. The optional second parameter is an array of various element settings. Everything in the settings-array is optional and has a sensible default.
E.g.: If you don't give a label-parameter htmlforms will use the name of the input as label. You can also add a required-parameter, so you won't be able to finish the form without filling a required-input.
Next comes the processing — The process-method is essential and the backbone of the htmlforms-class:
(Note: Make sure to call the process-method before any output, so the form is able to redirect itself.)
Now after processing the form we know if the submitted data is valid:
In the next posts about depage-forms we will introduce some extended features like: