Re: Forms validation and creation- easier solution?
| From: | Tony Marston | Date: | Sat, 23 May 2009 17:33:19 +0000 |
| Subject: | Re: Forms validation and creation- easier solution? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-293031@lists.php.net to get a copy of this message | ||
"Daniele Grillenzoni" <daniele@b2informatica.it> wrote in message
news:16.1E.28112.84CA51A4@pb1.pair.com...
> 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...
Your ideas are similar to mine, but I have a much more advanced
implementation which involves the use of a Data Dictionary. After building a
database table I import the structure into my data dictionary, then export
it to create a database table class and a table structure file. Still using
the data dictionary I can then build the family of transactions to maintain
that database table. This uses a standard set of page controllers and XSL
templates to build the HTML. So within 5 minutes I can run the transactions
to list, search, add, enquire, delete and update that database table without
having to write a single line of SQL or HTML. In most cases I don't even
have to write a single line of PHP. Is your method as fast as that?
All this functionality exists within the Radicore framework, so you can
download it and try it for yourself.
--
Tony Marston
http://www.tonymarston.net
http://www.radicore.org