Form Class by Matthew McNaney ------------------------------- Version History ------------------------------- 0.1 First draft April 20, 2006 Introduction ------------------------------ The PHPWS_Form class assists developers in the creation and display of web forms. Forms are made up of elements (text areas, check boxes, radio buttons, etc.). After constructing a form object, you add these elements and their properties individually. You complete can then complete the process calling the getTemplate function. The result can then be sent to phpWebSite's Template class for display. Getting Started ------------------------------ First construct a form object. $form = & new PHPWS_Form('form_name'); The 'form_name' is not required but you may need it should you have multiple forms on a single page. The default name is 'phpws_form'. Adding Elements ------------------------------ Each element is added using the 'add' function. If you developed for phpWebSite versions 0.10.x and under, this may be familiar: $form->add('name_of_element', 'type_of_element', 'value_of_element'); In phpWebSite 1.x and higher, you may find it easier to use the element type add functions instead. Here are the element types: text | textfield textarea submit button password file select | dropbox multiple radio | radiobutton check | checkbox hidden Here are the element type add functions addText | addTextField addTextarea addSubmit addButton addPassword addFile addSelect | addDropbox addMultiple addRadio | addRadioButton addCheck | addCheckBox addHidden With the element type functions, you just need the element name and value. Element Description -------------------------------------------- The following is a description of each element, how it's value is used, and an example of how it would display. Please note that for clarity and brevity, some attributes are not shown in the examples. text | text field Creates an single-line input type "text". The text field displays the value as the default text. // $form->addText('pet', 'dog'); textarea Creates a multiple-line "textarea" input. The text area displays the value as the default text. // $form->addTextarea('pet_story', 'Type story here...'); submit Creates an input type "submit" button. This button submits the form it is contained within. The value displays on the button face and is passed to the form when clicked. // $form->addSubmit('create_report', 'Create report'); Most of the time, a form only has _one_ submit button. As such, this function lets you only enter one parameter. If you do so, the name of the submit button becomes simply 'submit'. // $form->addSubmit('Create report'); The above ONLY works with "addSubmit" NOT with the "add" function. button Creates an input type "button". Value displays on the button face. Clicking the button does not submit the form. // $form->addButton('check_all', 'Check all boxes'); Button works like submit in regards to only needing one parameter. The default name for the button however is "button" not "submit". Like submit you MUST use "addButton" and not the "add" function. password Creates an input type "password". The value displays as hidden characters (usually "*******") in the password text box. The password itself IS viewable in the HTML source. // $form->addPassword('user_password'); Note: as a security measure, the password will NOT display by default unless the allowValue function is called like so: // $form->addPassword('user_password', 'qwerty'); $form->allowValue('user_password'); file Creates a text field and Browse button for the uploading of client files. Value is ignored. // $form->addFile('upload'); select | dropbox Creates a drop-down selector. Only one selection may be made per element, unlike the multiple element described below. The value passed to this element must be an array. Each row in the array, is an option. The array row's key is used as the option's "value". The element displays an array row's value as the option's content. Example: $sex = array('m' => 'Male', 'f' => 'Female'); $form->addSelect('sex', $sex); Displays: multiple Creates a multiple select drop-down. As the name implies, the user may choose multiple items in the list. Other than this difference, the parameters and functionality is identical to the "select" element. Example: $toppings = array('pr' => 'Pepperoni', 'm' => 'Mushrooms', 'yuck' => 'Anchovies'); $form->addMultiple('toppings', $toppings); Displays: "select" and "multiple" also handle matches differently. See the "Setting Matches" information later in this document. radio | radiobutton Creates an input type "radio" selector. Like the "multiple" element, the radio value is an array. Unlike "multiple", the array is not associative. The key of each array row is irrelevant, only the value is used. $beverage = array('pepsi', 'milk', 'h2o'); $form->addRadio('beverage', $beverage); Displays: Note: if you were looking at this in a browser, it would like this (with the zero representing a radio button): 0 0 0 To get this: 0 Pepsi 0 Milk 0 Water You would need to set the button's label (and add breaks in the template). That is covered in the "Setting Labels" section. Update: You can send an associative array if you use addRadioAssoc instead. $beverage = array('pepsi'=>'Pepsi-Cola', 'milk'=>'Moo Juice', 'h20'=>'Water'); $form->addRadioAssoc('beverage', $beverage); This function will send the keys to addRadio and the values to setLabel. check | checkbox Creates an input type "checkbox". // $form->addCheck('married', 'yes'); Unlike other elements, this element can receive either a string as a value (the most common occurence) OR an array. Use an array value for multiple checkboxes within the same element name: $foods = array('fried_chicken', 'pizza', 'ice_cream', 'hot_dogs', 'clams'); $form->addCheck('favorite_foods', $foods); Displays: If you use an array value, make sure to use a standard array (not associative) with natural key progression (i.e. 0,1,2,3, etc.). The form class may get confused otherwise. Note: Like the radio button above, you need to set the labels for the text next to the check box. Update: You can send an associative array if you use addCheckAssoc instead. $beverage = array('pepsi'=>'Pepsi-Cola', 'milk'=>'Moo Juice', 'h20'=>'Water'); $form->addCheckAssoc('beverage', $beverage); This function will send the keys to addCheck and the values to setLabel. hidden Creates an input of type "hidden". // $form->addHidden('current_id', 4); Hidden inputs do not actually display in your form but are passed on when submitted. Setting Labels (and the element Title) ---------------------------------------- Without labels, users won't know what they should be typing, selecting, or checking. Labels identify an elements function. Setting labels in not required, but using the "setLabel" is quite useful for a few reasons. First, setLabels links the descriptive text to the id of the element. This direct link enables extra browser usability. For example, you can click a checkbox's label as well as the check box itself to select it. Second, you can translate the label outside of the template. There are other reasons you will find on your own (code readibility, repeating template rows, etc.). Setting a label is simple. Just use the name of your element and the label text. $form->addText('username'); $form->setLabel('username', 'Enter your username'); This will display: Notice the "for" attribute: phpws_form_username. Now look at the input's id. They are the same. This is the linking I refered to earlier. The id value is a combination of the form (remember the default form name is 'phpws_form') and the element's name. Also notice the text input's title attribute. Normally if we did not set the label, the title would be the element's name. The title will usually appear when a user mouses over the input element. You can set the title yourself by using the setTitle function. $form->setTitle('username', 'The username field'); You would then get the below instead: To add labels to multi-valued elements, just set the label with an array: $foods = array('fried_chicken', 'pizza', 'ice_cream', 'hot_dogs', 'clams'); $food_labels = array('Fried Chicken', 'Gooey Pizza', 'Ice Cream', 'Foot Long Hot Dogs', 'Fried Clams'); $form->addCheck('favorite_foods', $foods); $form->setLabel('favorite_foods', $food_labels); The order of the label array must match the value order. Here is an example of radio button labels: $beverage = array('pepsi', 'milk', 'h2o'); $bev_label = array('Pepsi', 'Milk', 'Water'); $form->addRadio('beverage', $beverage); $form->setLabel('beverage', $bev_label); Single and multiple select elements do not need multiple labels. One label defines the whole select element. Matching elements --------------------------------------------- When using check boxes, radio buttons, and selects (multiple and single) you often need to set default values. In PHPWS_Form, this is done with the "setMatch" function. Single matches Check boxes with one value, radio buttons, and single selects use one value for matching. Here are some examples using earlier samples: Check boxes $form->addCheck('married', 'yes'); $form->setMatch('married', 'yes'); Displays Single select $sex = array('m' => 'Male', 'f' => 'Female'); $form->addSelect('sex', $sex); $form->setMatch('sex', 'm'); Displays Radio buttons $beverage = array('pepsi', 'milk', 'h2o'); $form->addRadio('beverage', $beverage); $form->setMatch('beverage', 'milk'); Multiple Matches Multiple check boxes and multiple selects use an array for matching. Check boxes $foods = array('fried_chicken', 'pizza', 'ice_cream', 'hot_dogs', 'clams'); $food_match = array('pizza', 'clams'); $form->addCheck('favorite_foods', $foods); $form->setMatch('favorite_foods', $food_match); Multiple select $toppings = array('pr' => 'Pepperoni', 'm' => 'Mushrooms', 'yuck' => 'Anchovies'); $top_match = array('m', 'yuck'); $form->addMultiple('toppings', $toppings); $form->setMatch('toppings', $top_match); Optgroups -------------------------------------- Optgroups define a set of options in a single or multiple select form element. The form class allows some limited optgroup capabilities. $form = new PHPWS_Form; // The list of elements for the select element $produce = array( 'apple'=>'Apple', 'grapes'=>'Grapes', 'strawberry'=>'Strawberries', 'celery'=>'Celery', 'carrot'=>'Carrot' ); $form->addSelect('produce', $produce); // The first parameter is the element I want to add the optGroup to. // The second parameter tells form which element starts the group // The last parameter is the group's label $form->setOptgroup('produce', 'apple', 'Fruit'); $form->setOptgroup('produce', 'celery', 'Vegetable'); $result = $form->getTemplate(); echo implode('', $result); Here is the output ------------------------------------------------------------------------
You can also use setOptgroup on a multiple form element: $form->addMultiple('produce', $produce); $form->setOptgroup('produce', 'apple', 'Fruit'); $form->setOptgroup('produce', 'celery', 'Vegetable'); setOptgroup will fail on any other type of element. Creating your template --------------------------------------- Once you have plugged in all your form elements, you can call the "getTemplate" function. $template = $form->getTemplate(); The template variable is an associative array. To get your final form content, you just need to send it to PHPWS_Template: $content = PHPWS_Template::process($template, 'my_module_title', 'template_file.tpl'); You can now display the result of the process function. If you are unsure how templates work in phpWebSite, please read template.txt in the docs directory. Now you are ready to create your form template. Two tags frame your template module: START_FORM and END_FORM. All other form tags should appear between these two tags. The other form tags are derived from your form element names. For example: $form->addHidden('command', 'save_pet'); $form->addText('pet'); To display this text field we just need to add a "pet" tag. {START_FORM} {PET} {END_FORM} Notice that all the tags are capitalized. This is done automatically. Make sure you don't have two form elements named the same with only differences in case. (e.g. my_variable vs. My_Variable: MY_VARIABLE will be the tag). Notice that we do not have a tag for the "command" hidden element. All hidden elements are included in the START_FORM tag. Since they are never seen, there is no reason to have tags in the template for hidden variables. Now lets add a label: $form->setLabel('pet', "Your pet's name"); We just need to append the form tag with "_LABEL": {START_FORM} {PET_LABEL}