1. Dave Gauer
  2. phormalWear

Overview

HTTPS SSH

phormalWear

"Just trying to make working with HTML forms slightly less repulsive. An extremely lightweight and general-purpose utility created with love."

By Dave Gauer

Tiny Example

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.

*Note: while phormalWear has no problem with two different forms having elements with the same name, the elements will also be given the same IDs, which is technically forbidden by HTML. In practice, browsers do not balk at this. But you should be aware that this is not desirable and could be a problem when attempting to access DOM elements with JavaScript. (phormalWear does, however, forbid adding two elements with the same name to the same form!)

Next, set the URL to which the form should submit itself.

$user_info->submitTo('save-user-profile.php');

Your form object is now fully setup and you can begin adding form elements to the object.

Adding Elements

TODO:

  • text fields
  • password fields
  • checkboxes
  • radio buttons
  • select boxes
  • textareas

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.

TODO:

  • the form tag (with hidden fields)
  • most elements are the same
  • radio buttons
  • labels
  • submit button

Validating forms and displaying validation errors

TODO

  • 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

TODO

  • element array syntax

Misc Functions

TODO

  • hasElement()
  • addProperty()