On 20/05/2009 9.03, Angelo Zanetti wrote:
Hi all.
We have done quite a few projects and we are looking to find better ways to
implementing forms.
Forms seem to be quite time consuming and repetitive.
Generally are there any classes or libraries that will assist with:
1. Easy creation of forms (fields and layout)
2. Validation of specific fields within the forms (server side not JS)
3. Decrease in time required to setup the forms pages
any other comments are welcome.
Thanks in advance
Angelo
Elemental
http://www.elemental.co.za<http://www.elemental.co.za/>
Dynamic Web and Mobile Solutions
Personally I created a little Class that handles it.
First I create an array of associative arrays, every associative array
represents an input of an abstract type (similar to the webforms,
meaning i have stuff like type="telephone").
Stuff like
$inputs[]=array(
"name" => "foobar",
"required" => true,
"type" => "string", // text is reserved to textarea
"label" => "Foo Bar",
"error_required" => "you need to provide a Foo Bar value, dummy",
"group" => "main"
);
etc.
Then I create an array for groups.
$group["main"] = array(
"type" = "block", // types are either block which means div, set
which means fieldset, paragraph which means <p> or a raw html opening tag.
"parent" = NULL //optional of course, if set to the name of another
group, then the group becomes a child of the referenced group.
)
Then I create an associative array of options for the form.
Finally, I call the class constructor with the three arrays as params.
The class provides me with a few nifty functions:
* toHtml();
(do I need to explain?)
* toArray();
Returns the inputs, options, and groups inside a single array, with the
value altered when necessary
* wasSubmitted();
Does some guesswork to see if the form was submitted, there's a lot of
smart automagicness inside.
* runAutoChecks();
Runs the checks he can, like the validity of emails in 'type' = 'email'
inputs, pattern validation for input with a set pattern, required
inputs, fills the error array with error messages, sets class[]='error'
to wrongly filled inputs...
* wasValidInput();
Returns true if none of the autochecks or eventual manual checks
returned an error.
And it works like this:
[...]
if ($form->wasSubmitted()){
$form->runAutoChecks();
/* Additional non-automatable controls */
// None in this case
if ($form->wasValidInput()){
// success, do stuff
} else {
// show errors and form again
}
} else {
echo $form->toHtml();
}
There are other things I didn't list, like the fact that ever input has
options to specify a wrapper (class and id are associated to the wrapper
if it's defined), the form encoding automatically changes in case of a
file input, etc etc etc...
The types are abstracted enough that one could easily make a function
that automatically creates the code for a first draft of the forum out
of the db schema of an eventual table. Of course you'd have to provide
error messages, remove unnecessary inputs, adding new ones...
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php