PHP Classes

File: SForm_usage.php

Recommend this page to a friend!
  Classes of Seth Price  >  SForm  >  SForm_usage.php  >  Download  
File: SForm_usage.php
Role: Example script
Content type: text/plain
Description: A complete example
Class: SForm
Generate and validate Web forms
Author: By
Last change:
Date: 16 years ago
Size: 3,309 bytes


Class file image Download
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Testing Seth's Form</title>

 * Require SForm
require_once 'SForm.php';

//Create a new form
$form = new SForm('subNew');

 * Add the data type selection
//Create a new element directly
$dataTypeElm = new SForm_Elm_Select('dataType', 'Data Type');

//Add an option and mark it as invalid, but selected by default
$dataTypeElm->addOption('Select a Data Type', -1, array('selected'=>'selected'));

//Add regular options
$dataTypeElm->addOption('Map', 1);
$dataTypeElm->addOption('Observation', 2);

//Add a disabled option
$dataTypeElm->addOption('Study', 3, array('disabled'=>'disabled'));

//Add the element to the data set in a fieldset

 * Location selection
//Create a new element via a factory method
$areaElm = $form->addElement('select', 'area', 'Area');

//Add an option and mark it as invalid
$areaElm->addOption('Select an Area', -1, array('selected'=>'selected'));

//Create options and group them
$areaElm->addOption('Canada', 1);
$areaElm->addOption('United States', 2);
$areaElm->addOption('Mexico', 3);

$areaElm->addOptGroup('North America', array(1,2,3));

$areaElm->addOption('Brazil', 4);
$areaElm->addOption('Colombia', 5);
$areaElm->addOption('Peru', 6);
$areaElm->addOption('Argentina', 7);

$areaElm->addOptGroup('South America', array(4,5,6,7));

//These aren't in a group
$areaElm->addOption('Germany', 8);

$areaElm->addOption('New Area (see below)', -2);

 * Category selection
//Create a fieldset for the checkboxes
$fs = $form->addElement('fieldset', null, 'Category');

//Add the checkboxes to the fieldset
$fs->addElement('checkbox', 'forest', 'Forests');
$fs->addElement('checkbox', 'ag', 'Agriculture');
$fs->addElement('checkbox', 'water', 'Water');

//Set these to checked by default
$fs->addElement('checkbox', 'climate', 'Climate')->setDefault(true);
$fs->addElement('checkbox', 'health', 'Health', array('checked'=>'checked'));

 * Language selection
$langElm = new SForm_Elm_Select('lang', 'Language');

$langElm->addOption('English', 'en');
$langElm->addOption('Spanish', 'sp');
$langElm->addOption('Portuguese', 'pr');
$langElm->addOption('German', 'gr');


 * New location creation fieldset
//Create a fieldset
$fs = new SForm_Fieldset(null, 'New Area');

//Add some various input boxes
$fs->addElement($newNameElm = new SForm_Elm_Text('new_name', 'Name'));
$fs->addElement(new SForm_Elm_Text('new_top', 'Top'));
$fs->addElement(new SForm_Elm_Text('new_right', 'Right'));
$fs->addElement(new SForm_Elm_Text('new_bottom', 'Bottom'));
$fs->addElement(new SForm_Elm_Text('new_left', 'Left'));

//Create a custom boolean rule
require_once 'SForm/Rule/Boolean.php';
$newNameElm->addRule(new SForm_Rule_Boolean(
'If you are creating a new area, you must fill in its name.',
$newNameElm, $areaElm),
'%s == "" && %s == -2'));


 * Submission button
$form->addElement('submit', 'sub_new', 'Begin Submission');