Keith Devens .com |
Wednesday, July 23, 2008 | ![]() |
| We ought always to deal justly, not only with those who are just to us, but likewise to those who... – Hierocles | ||
Formation: web form automation library for PHPversion 0.9.4 <- MEANS BETANote: this code currently has a small bug in PHP 5. It's already fixed in the current version. I'll update this version soon. IntroductionFormation is a web form automation library. Given a form definition, it enables easy validation, processing, and generation of an HTML form. This documentation is not complete (beta, after all). Currently, it just gives an example of what this code does, but it doesn't give full documentation of all of its features. For now, you'll have to read the code. By the way, you may find this interesting: All the code you see on this page is the exact same code that's actually run on the page to display and process the form below. I store the code in strings and use them as the source for the highlighted source listings below, and I also eval them to execute the code they contain. ExamplesThe best way to understand this is to see it in action. Here's one of the simplest forms possible, a login form. First, you include the Formation library:
<?php require_once('Formation.php');?>
Then, you define your form class.
<?php
As you can see, you create a class that inherits from ActionForm. Every form must override the construct and process functions. construct() defines all the fields in your form. process() is called automatically if the submitted form validates (as determined by the validate() method). Note that the code in the process() function is commented out because this code actually runs on this page if you submit the example login form, and I don't want you leaving session variables around :) Here's the code you'd use to display the form defined above.
<?php
This displays the resulting form: The above form actually works (try it!). You don't *have* to use the printDefaultForm() function in case you'd like to have more control over how the form is laid out, but I've tried hard to enable most common cases to be handled by printDefaultForm() without having to do more manually. Finally, the code that actually submits the form is as follows:
<?php $form = &new Formation($name='logIn');?>
Formation, upon instantiation, creates a logIn instance and submits it if the form has been submitted. You need to use the $name hack because PHP's references suck. It's not a big deal. Consider it a named parameter like they have in Python. Just make sure you're not already using the $name variable for something else. If you are, use another variable, since the name of what I call $name doesn't matter. Download the sourceHere's the raw source code (just one file), and if you want pretty colors, here's the syntax highlighted version. Remember... beta! Contact meIf you find any bugs or have any feature requests, or just want to say "hi" or "thanks", please drop me a line. Potential future features (to do)
Change History
Page last edited: November 24, 2005 (utc) |
IndexA B C D E F G H I J L M N O P R S T U V W X All pagesABC
DEFGHIJLMNOP
RS
T
UVWXGenerated in about 0.058s. (Used 5 db queries) |
