ValidForm Builder

Easy and safe XHTML 1.0 strict forms with validation!

Contents

ValidForm→addField

ValidForm→addField — Adds a new field object to the ValidForm object


Description

addField (string $name, string $label, int $type[, array $validationRules[, array $errorHandlers[, array $meta[, bool $justRender]]]]);

Adds a new field object to the ValidForm object.

Parameters

  • name
    Name (and ID) of the new field
  • label
    Label of the field. Will be shown in the HTML <label>-tag
  • type
    See Predefined Constants for all possible field types.
  • validationRules
    See: Validation Rules for all possible validation rules.
  • errorHandlers
    See: Error Handlers for all possible error handlers.
  • meta
    See: Meta for all possible meta data.

Return values

Returns type dependent one of these objects:

case VFORM_STRING:
case VFORM_WORD:
case VFORM_EMAIL:
case VFORM_SIMPLEURL:
case VFORM_CUSTOM:
case VFORM_CURRENCY:
case VFORM_DATE:
case VFORM_NUMERIC:
case VFORM_INTEGER:

case VFORM_PASSWORD:

case VFORM_CAPTCHA:

case VFORM_TEXT:

case VFORM_FILE:

case VFORM_BOOLEAN:

case VFORM_RADIO_LIST:
case VFORM_CHECK_LIST:

case VFORM_SELECT_LIST:

default:

Examples

A disabled field

$objForm->addField("name", "Edit name", VFORM_STRING,
   array(
       // This field is not required; it is disabled
       "required" => false
   ),
   array(
       // Just in case someone tries anything funny
       "type" => "Invalid data submitted." 
   ),
   array(
       // The text field is disabled
       "disabled" => true, 
       // Default value of the text input field
       "default" => "No name. Fill out the registration form first.", 
       "tip" => "<a href=\"/register\">Click here</a> to register your name."
   )
);

Field with custom regexp validation

$objForm->addField("summary", "Summary", VFORM_TEXT,
    array(
        // This field is required
        "required" => true, 
        // The custom regular expression for validation
        "validation" => "((?:[a-z][a-z0-9_]*))"
    ),
    array(
        // In case someone didn't understand it...
        "required" => "This is a required field.", 
        // The data didn't pass the (custom) regexp validation
        "type" => "You've inserted invalid data." 
    )
);

Minimum and maximum characters

$objForm->addField("limited_input", "Limited input", VFORM_STRING,
    array(
        "required" => true,
        "minLength" => 5,
        "maxLength" => 10
    ),
    array(
        "type" => "You've used invalid characters.",
        "required" => "This field is required.",
        // This error message uses sprintf to insert 
        // the minimum amount of characters.
        "minLength" => "You have to fill in at least %s characters!", 
        // Same as minLength
        "maxLength" => "You have inserted to many characters. Maximum of % allowed." 
    )
);

Note that the arrays are not required. That's why we left the meta array out of the example.