Form: An HTML Form Fields Generation Utility
Introduction The form package is a utility for generating html forms. A form object command saves the programmer from typing the cumbersome html code of input elements, working out a solution for better standardization and readability of the code. form requires that only the minimum necessary to distinguish the element is typed, greatly simplyfing the development of forms. Options to the command are treated as a list of parameter-value pairs that become the defaults for the corresponding attributes of the form. A form object has specialized menthods to generate all of the standard input fields, i.e. text, password, hidden, generic button, submit or reset buttons and image. form creates select input fields, radiobutton and checkbox boolean options groups. Also new inputs introduced with HTML5 are supported: color, date, datetime, datetime-local, email, file, month, number, range, search, tel, time, url, week. Other input elements can be generated using the general purpose 'field' method.
form a Tcl command object for creating HTML forms form form_name -option1 value_1 -option2 value_2 ... creates and returns a new Tcl command named . Options -method post|get The http method for sending the form data back to the server. Possible values are get or post -name form_name a name for the form being created: this value becomes the value of the attribute 'name' in the <form> tag. -defaults default_values an array of default values to be assigned to the fields of the form. Every name in the array is matched with an input field, when a given field gets added to the form it is initialized with the value of the corresponding variable in the array. This option works well in conjuction with the load_response command of Rivet when default values come from another form. -action URL The URL the data are being sent to. If no -action switch is specified the data are sent to the form's URL. Form Object Commands Form object commands follow the usual syntax of Tcl commands with a subcommand argument playing the role of a switch among various functionalities of the command. Form objects also need the name parameter which is to become the value of the 'name' attribute in an input field. This argument is the key that has to be used by the server-side script to retrieve the input field value. form_object subcommand name -option1 value1 -option2 value2 ... Options passed to a subcommand are copied into the tag as attribute="value" pairs. Some subcommands (e.g. form, radiobuttons and checkboxes) treat specific options in a way that fits the specific organization and function of these fields. Exceptions to this general syntax are the field and end subcommands. field is an abstract input field creation method and requires an additional parameter specifiyng the type of field to create. Every concrete input field generation command uses this subcommand internally to print the final html. Subcommands start name -method get | post -name form_name -defaults default_values -action URL args Print the <form> tag with all its attributes. This command must be called as first in the form generation process. The following is a sample of code creating a form named 'formname' whose data will be sent via the GET method. Initial form fields values will be obtained from array response form myform -defaults response -method get -name formname myform start myform text text_entry -size 20 myform select option_selected -values {opt1 opt2 opt3 opt4} myform submit submit -value Search myform end The code prints a form that sends a text entry content and the option value associated with a radiobutton. The URL of the server script is the same that created the form. Use the -url option to specify a different url. Options -method post|get The method to be used to encode the form data. Possible values are get or post -name form_name a name for the form being generated: this value becomes the value of the attribute 'name' in the <form> tag. -defaults default_values an array of default values to be assigned to the fields of the form. Every name in the array is matched with an input field, when a given field gets added to the form it is initialized with the value of the corresponding variable in the array. This option works well in conjuction with the load_response command of Rivet when default values come from another form. -action URL The URL the data will be sent to. If no -action switch is specified the data are sent to the form's URL. end Print the </form> closing tag. This command must be called last in the form generation process field name type args Print a field of the given type and name, including any default key-value pairs defined for this field type and optional key-value pairs included with the statement Options -opt1 val1 Option description radiobuttons name -values values -labels labels args the radiobutton creates a whole radiobutton group with the values and labels specified in the argument list. If no -labels switch is passed to the subcommand the values are printed as labels of the radiobutton. Options -values values_list List of values associated with the radiobuttons to be displayed -labels labels_list List of labels to be printed with every radiobutton. There must be a label for every radiobutton Example: form myform -defaults response -method get -name formname myform start myform text text_entry -size 20 myform radiobuttons fruit -values {big medium small} \ -labels {Watermelon Orange Strawberry} \ -class myradiobclass myform submit submit -value Search myform end will print the following HTML code. <input type="radio" name="fruit" class="myradiobclass" value="big" />Watermelon <input type="radio" name="fruit" class="myradiobclass" value="medium" />Orange <input type="radio" name="fruit" class="myradiobclass" value="small" />Strawberry if the response array has a variable for the name 'fruit' the corresponding radiobutton field is automatically checked. The options values and labels are used internally and don't get into the tag attributes. If a labels option is not given, labels are assigned using the values list. checkbox name -label label -value value args The checkbox subcommand emits a checkbox type input field with the name, label and value attributes set according to the parameters passed to the subcommand. Example: form myform -defaults response -method get -name formname -action <form_url> myform start myform checkbox options -value opt1 -label "Option 1" myform checkbox options -value opt2 -label "Option 2" myform checkbox options -value opt3 -label "Option 3" myform checkbox options -value opt4 -label "Option 4" myform submit save_tps -value "Send Options" myform end myform destroy Provided opt2 was in response array (in the list valued 'options' variable) that initialized the form, the output would look like this <form action="<form_url>" method="get" name="formname"> <input type="checkbox" name="options" id="autogen_1" label="Option 1" value="sopt1" /><label for="autogen_1">Option 1</label> <input type="checkbox" name="options" id="autogen_2" label="Option 2" value="sopt2" /><label for="autogen_2">Option 2</label> <input type="checkbox" name="options" id="autogen_3" label="Option 3" value="sopt3" /><label for="autogen_3">Option 3</label> <input type="checkbox" name="options" id="autogen_4" label="Option 4" value="sopt4" /><label for="autogen_4">Option 4</label> <input type="submit" name="submit" value="Send" /> </form> checkboxes name -labels labels_list -values values_list args The checkboxes is an extended form of the checkbox subcommand. checkboxes prints as many checkboxes as the number of elements in the labels_list argument Options -values values_list List of values associated with the checkboxes to be displayed -labels labels_list List of labels to be printed with every checkbox. There must be a label for every checkbox Example: form myform -defaults response -method post -action <form_url> myform start myform checkboxes options -values {opt1 opt2 opt3 opt4} -labels {"Option 1" "Option 2" "Option 3" "Option 4"} myform submit save_tps -value "Send Options" myform end myform destroy will print the following HTML code <form action="<form_url>" method="post"> <input type="checkbox" name="options" id="autogen_1" label="Option 1" value="opt1" /><label for="autogen_1">Option 1</label> <input type="checkbox" name="options" id="autogen_2" label="Option 2" value="opt2" /><label for="autogen_2">Option 2</label> <input type="checkbox" name="options" id="autogen_3" label="Option 3" value="opt3" /><label for="autogen_3">Option 3</label> <input type="checkbox" name="options" id="autogen_4" label="Option 4" value="opt4" /><label for="autogen_4">Option 4</label> <input type="submit" name="save_tps" value="Send Options" /> </form> password name args Same as text, but the input is obfuscated so as not to reveal the text being typed hidden name args hidden input element: typicall embedded in a form in order to pass status variables. submit name args emits the code for a classical HTML submit button. Example: the following code form myform -defaults response -method get -name feedsearch myform start myform submit submit -value Search Would emit a form like this <form...> <input type="submit" name="submit" value="Search" /> </form> button name args emits the code for a button field having name as name reset name args Classical HTML reset button that resets the input fields back to their initial values image name args Emits an image input field radio name args Emits a radiobutton input field color name args Emits an HTML 5 "color" form field date name args Emits an HTML 5 "date" form field datetime name args Emits an HTML 5 "datetime" form field datetime_local name args Emits an HTML 5 "datetime_local" form field email name args Emits an HTML 5 "email" form field file name args Emits an HTML 5 "file" form field month name args Emits an HTML 5 "month" form field number name args Emits an HTML 5 "number" form field range name args Emits an HTML 5 "range" form field search name args Emits an HTML 5 "search" form field tel name args Emits an HTML 5 "tel" form field time name args Emits an HTML 5 "time" form field url name args Emits an HTML 5 "url" form field week name args Emits an HTML 5 "week" form field