Download the PHP package flsouto/htform without Composer
On this page you can find all versions of the php package flsouto/htform. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package htform
HtForm
Installation
To install this package use composer:
Overview
Forget about everything you've seen so far regarding form building tools and libraries. The approach you are going to see here is something totally new and straightforward:
- No need to instantiate and/or configure tons of objects.
- No need to define and connect the form to a model.
- No need to have xml or yml files defining how the form is supposed to behave.
- No need to define your own form and field classes.
- No need to setup a "renderer engine" before the form can be rendered.
- No need to get the form builder from a DIC (Dependency Injection Container)
Usage
All you have to do is this: create a form object, add some fields and echo it:
Outputs:
That is pretty easy, huh?
Adding labels to fields
By default fields don't have a label but you can add one by calling the label
setter on a field instance:
Outputs:
Making field labels inline
By default a field's label is a block level element. You can change that by passing the inline => true
flag:
Outputs:
Defining Placeholders
You can use the placeholder
setter which is really just a shortcut for setting the field's 'placeholder' attribute:
Outputs:
Other attributes can be set by passing an associative array to the field's attrs
method.
Adding different types of fields
A form is all about attaching a bunch of fields for taking user input. Different field types are available in this library:
Outputs:
Each field type is defined in its own class. These classes have a repostory of their own too. For more info on the fields API, take a look in the following repositories:
-
HtField - Abstract base class for all fields
- HtWidget - Base class for all widget types (extends HtField)
Notice: when adding the "upload" field the form will automatically have enctype=multipart/form-data and method=POST
Make all fields inline at once
Sometimes you need to build "quick search" type of forms in which all fields are rendered
in a single line. For this you can use the inline
method on the form instance:
Outputs:
Make all fields readonly at once
Sometimes you want to reuse the same form layout/structure but in readonly mode. In this case
you can call the readonly
method of the form object in order to disable editing on all fields:
Outputs:
Changing Form Attributes
By default, the form is rendered with the following attributes:
- method: GET
- action: ?
- id: random id
But these can be changed easily by calling the corresponding setters on the form instance:
Outputs:
Populate form with data
In order to populate the form with data you have to call the context
method on the form object and pass an associative array to it.
The keys of the associative array must match the names of the fields you added to the form:
Outputs:
Processing form submission
Processing a form is all about populating it with incoming data and calling the process
method.
It is also good practice to check if a flag is present indicating that the form has been sent. Check this out:
Outputs:
Validate form submission
You can add validation rules to be checked upon form submission. Validation rules are added on a per-field basis and yield error messages when something is wrong. These errors are available in the result object returned by the process method. Take a look:
Outputs:
Namespacing form fields
The data structure behind your form doesn't have to be flat. You can group fields into logical sections, see example below:
Outputs:
Unfold form data
As you may have seen in the last example, even though we can process complex data structures sent over a request,
the data is extracted in a flat format. Luckily, the result object provides a method called unfold
which can be used to reconstruct
the data into a multidimensional array:
Outputs:
All versions of htform with dependencies
flsouto/htbutton Version ^1.0
flsouto/hthidden Version ^1.0
flsouto/httextin Version ^1.0
flsouto/httextar Version ^1.0
flsouto/htcheckb Version ^1.0
flsouto/htselect Version ^1.0
flsouto/htupload Version ^1.0