"Just trying to make working with HTML forms slightly less repulsive. An extremely lightweight and general-purpose utility created with love."
By Dave Gauer
An extremely simple example, a page that submits a single text field to itself, displaying the submitted text:
<?php // example0_tiny.php require_once('phormalwear.php'); $form = new PhormalWear('Save My Favorite Color'); $form->submitTo('example0_tiny.php'); $form->add('text', 'color', 'Favorite Color'); ?> <html><body> <?php if($form->wasSubmitted()): ?> <p>Your favorite color is <?php echo $form->valueOf('color'); ?></p> <?php else: ?> <!-- <form> starts here --> <?php $form->displayFormTag(); ?> Enter your favorite color: <?php $form->display('color'); ?> <?php $form->displaySubmitButton(); ?> </form> <?php endif; ?> </body></html>
Creating a new phormalWear object
First, be sure you can access the PhormalWear class by either importing with
require_once() or using
spl_autoload_register() to automatically import the class as needed.
Then create a new form object by creating a new instance of PhormalWear and give the form a name. The name can be anything, but as a handy shortcut, you may display a submit button with this name on it - so descriptive action names like "Submit My Data" may be preferable. Though you may never intend to display it anywhere in the browser, this name is required. phormalWear uses it to create a unique instance name.
require_once('phormalwear.php'); $user_info = new PhormalWear('Save Profile');
Multiple forms may exist on the same page*. Each phormalWear form object knows if it has been submitted apart from any other.
Next, set the URL to which the form should submit itself.
Your form object is now fully setup and you can begin adding form elements to the object.
- text fields
- password fields
- radio buttons
- select boxes
Displaying forms in HTML
The form element display functions are completely optional - you're free to write out any or all of the form elements yourself. But phormalWear makes them easy because it keeps track of their values and makes displaying the various types relatively homogeneous.
- the form tag (with hidden fields)
- most elements are the same
- radio buttons
- submit button
Validating forms and displaying validation errors
- built-in validation functions
- manually adding validation errors
- checking for validation errors
- getting the error list
- displaying errors for a field
Grouping elements with element arrays
- element array syntax