📄 05-form.sgml
字号:
<!-- $Id: 05-form.sgml,v 1.1.1.1 2000/04/17 16:40:04 kk Exp $ --><sect1>Form<p>The form class (sometimes called OOH Forms) is a conveniencelibrary for dealing with html forms. It provides Javascript andserver-side form validation, and is customizable and extensible.<sect2>Using OOH Forms<p>The OOH Forms library consists of five files: <tt/oohforms.incof_checkbox.inc of_radio.inc of_select.incof_text.inc of_textarea.inc/. <tt/oohforms.inc/automatically includes the others. You may wish to modify this so youcan manually include the files for just the form elements you use. Oryou may wish to cut and paste the contents of the element files into<tt/oohforms.inc/ to save the overhead of multiple includes.Determining the appropriate configuration of the files for your siteis left an exercise for the reader; for most purposes<tt/require("oohforms.inc")/ will suffice.<p>In general, the structure of a page that uses oohforms is asfollows:<tscreen><code>require("oohforms.inc"); // include the library$f = new form; // create a form object$f->add_element(...); // set up form elements$f->add_element(...); $f->add_element(...); if ($submitname) // Is there data to process? if ($err = $f->validate()) { // Is the data valid? echo $err; // No; Display error $f->load_defaults(); // Load form with submitted data else { /* Process data */ // Data ok; Do something with it }$f->start(...); // Start displaying form$f->show_element(...); // Show elements$f->show_element(...);$f->show_element(...);$->finish(); // Finish form</code></tscreen><p>There are obviously many variations on this theme, but that coversthe basics. Specific methods are documented below.<descrip><tag>start($jvsname,$method,$action, $target)</tag><p>Outputs the initial <tt/<form>/ tag and sets up some initialstate needed by the class. All of the arguments are optional, thoughyou'll usually want to use at least one in order to get Javascriptvalidation. <tt/$jvsname/ is an arbitrary string used to link theJavascript to the form; if it is empty (the default), no Javascriptvalidation is provided. <tt/$method/ is the HTTP method used to submit the form; default is <tt/"POST"/. <tt/$action/ is the URL that receivesthe form submission; default is <tt/$PHP_SELF/. <tt/$target/ is theframe target for the form results; default is <tt/_self/.<tag>finish($after,$before)</tag> <p>Outputs the any hidden fields that were added to the form, thefinal <tt></form></tt> tag, then the Javascript validationfunction (if necessary). <tt/$after/ and <tt/$before/ are bothoptional; if either is a nonempty string it is output as additionalJavascript to be run on submission of the form, either before or after the validation code. Though the order of the arguments may seemcounterintuitive, it makes the common case easier to type; in generalyou'll want to wait until after the validation has taken place to doanything fancy with Javascript. Note that unlike with validation, OOH Forms has no way of giving you server side functionality equivalent to the Javascript you use here.<tag>add_element($element)</tag><p><tt/add_element/ is used to define the attributes of aparticular form element so that the other class methods can use andmanipulate it properly. <tt/add_element/ takes exactly oneargument: an associate array whose key value pairs are used todefine the form element type and it's various attributes. Some ofthese attributes correspond to html attributes, while others areneeded for the value added features of oohforms. The attributes andthe syntax and semantics of the values they take are documented below; note that not all element types use all of the attributes<descrip><tag>type</tag> <p>The type of element this is; can be <tt/"submit"/, <tt/"hidden"/,<tt/"text"/, <tt/"textarea"/, <tt/"select"/, <tt/"radio"/, <tt/"checkbox"/, or <tt/"file"/.<tag>name</tag><p>A string naming this element. This name will be used as anargument to other methods and will be the <tt/name=""/ value in thegenerated html (and hence the variable name in PHP). <bf/Do not/append <tt/[]/ to the name if you want an array valued element; setthe <tt/multiple/ attribute instead.<tag>value</tag><p>The default value of this form element. If the form element hasthe <tt/multiple/ attribute set, <tt/value/ can be an array. If thethis is a <tt/select/ element, <tt/value/ can refer to either thetextual name (<tt/label/ in the <tt/options/ array) or the submissionvalue (<tt/value/ in <tt/options/).<tag>multiple</tag><p>A flag to tell oohforms to assume this element is array valued.The use of this flag is most straightforward with <tt/select/ elements, but it can be use with <tt/text/ and <tt/checkbox/ elements as well.See the <tt/show_element/ documentation for more informationabout how oohforms deals with such elements.<tag>extrahtml</tag><p>Extra html code that is inserted verbatim into the opening formtag. For <tt/select/ elements, the extra html goes into the<tt/select/ tag, not the <tt/option/ tags.<tag>size</tag><p>For <tt/text/ elements, used to set the html size attribute thatgives the width in characters of the text entry box. For <tt/select/elements, the size (number of options visible at once) of theselection box. Validation is only performed on <tt/select/ elementsif <tt/size/ is set to 1, since <tt/select/ validation doesn't makemuch sense if you can see multiple options at once. For <tt/file/elements, the maximum size file upload to accept.<tag>pass</tag><p>If set for a <tt/text/ element, renders the html as a passwordelement, i.e. entry displays as asterisks.<tag>src</tag><p>If set for a <tt/submit/ element, convert to an image element anduse the value of <tt/src/ as the source URL for the image.<tag>maxlength</tag><p>Used verbatim as the maxlength html attribute in <tt/text/elements.<tag>minlength</tag><p>If <tt/length_e/ is set, this is the minimum length<tt/text/ element entry accepted by the validator.<tag>length_e</tag><p>If set, validate the <tt/text/ element to assure it has at least<tt/minlength/ characters. The value of <tt/length_e/ is theerror string to be used in the event of failed validation.<tag>valid_e</tag><p>If set, perform validation on a <tt/text/, <tt/radio/, or<tt/select/ element. For a <tt/text/ element, validation assures amatch with <tt/valid_/ regex. <tt/radio/ element validationassures that one of the options in the group has been chosen.<tt/select/ validation only works for <tt/select/ elements with<tt/multiple/ unset and <tt/size/ equal to 1; the validator will notaccept the first option of accept menu, assuming that it is some sortof prompt (e.g. "Please select an item"). In all cases, the value of<tt/valid_e/ is the error string used for failed validations.<tag>valid_regex</tag><p>Regular expression used to validate entry into a test field if<tt/valid_e/ is set. Note that if you must use ^...$ if youwant the regex to match the whole entry.<tag>icase</tag><p>If set, regex matching is case insensitive.<tag>checked</tag><p>Only used for a <tt/checkbox/ element that does not have<tt/multiple/ set. If <tt/checked/ is set, the element will displayas checked.<tag>rows</tag><p>Used verbatim as the <tt/rows=/ element in a <tt/textarea/ element.<tag>cols</tag><p>Used verbatim as the <tt/cols=/ element in a <tt/textarea/ element.<tag>wrap</tag><p>Used verbatim as the <tt/wrap=/ element in a <tt/textarea/ element.<tag>options</tag><p>Array of options to be displayed in a <tt/select/ element. If theelements of the array are simple values (strings or numbers), they are simply displayed verbatim and used as the value for that particularoption. The elements may themselves be associate arrays with keys<tt/"label"/ and <tt/"value"/. In that case, the value of<tt/"label"/ is displayed and the value of <tt/"value"/ is used onsubmission.</descrip><p>Examples:<tscreen><code>$f->add_element(array("type"=>"text", "name"=>"foo", "valid_regex"=>"^[a-z]*$", "valid_e"=>"Letters only", "icase"=>1, "value"=>"bar"));$f->add_element(array("type"=>"checkbox", "name"=>"compress", "multiple"=>1));$f->add_element(array("type"=>"textarea", "name"=>"comment", "rows"=>6, "cols"=>40, "value"=>""));$o = array(array("label"=>"Please Select","value"=>0), array("label"=>"Apple","value"=>1), array("label"=>"Orange","value"=>2), array("label"=>"Pear","value"=>3), array("label"=>"Grape","value"=>4));$f->add_element(array("type"=>"select", "name"=>"menu", "options"=>$o, "size"=>1, "valid_e"=>"Please select a fruit", "value"=>"apple"));</code></tscreen><tag>show_element($name,$value)</tag><p>Outputs the form element named <tt/$name/. Usually, the secondargument is not used. It is always necessary for <tt/radio/ elementsand <tt/checkbox/ elements with the <tt/multiple/ attribute set, since many of these may have the same name. It also must be used for<tt/submit/ elements to label the submission button; the <tt/value/attribute is not used for <tt/submit/ elements. For other elementsthat may be array valued (notably <tt/text/ elements) multiple callsto show_element will show successive values.<tag>load_defaults($element_list)</tag><p>Sets the default value of form elements to the value of the PHPvariables with the same name. This is generally used to redisplay aform with the same values which were submitted. The argument isoptional; if given it is an array of element names; only theseelements ares affected.<tag>validate($result,$element_list)</tag><p>Validates a form submission. If all of the elements are valid,return <tt/$result/, otherwise return the relevant error message (the<tt/valid_e/ or length_e attribute of some formelement). <tt/$result/ defaults to <tt/false/. The second argumentis also optional; it is an array of names of elements to validate.<tag>freeze($element_list)</tag><p>Freezes the form elements whose names are given in the array passed as the argument. If no argument is given, freeze all of theelements. Frozen elements are rendered as plain, static html ratherthan form widgets. This static rendering is accompanied byappropriate hidden elements to simulate the affect of using theunfrozen version of the element.</descrip><sect2>Customizing OOH Forms<p>Since OOH Forms is object oriented, it can be easily customized byextending the classes that define the element types. In general, youmust make sure your derived class has a constructor and you mayoverride any of the self_* functions of<tt/of_element/. The source for the existing elements is thebest documentation for how to do this properly, but a few generalnotes follow.<descrip><tag>self_show($val,$which)</tag><p>Display an instance of this element unfrozen. <tt/$val/ is the<tt/$value/ argument of <tt/show_element/ if there was one;<tt/$which/ can be used as an index for array valued elements; it isequal to the number of times <tt/show_element/ has been calledfor this element previously. This function must return the number ofhidden tags output.<tag>self_show_frozen($val,$which)</tag><p>Display an instance of this element frozen. In addition to thehtml to show the frozen element, you must output tags for hiddenfields to duplicate the effect of submitting an unfrozen element ofthis type. The function must return the number of hidden tags output;<tag>self_validate($val)</tag><p>Validate <tt/$val/ for this element. If it is valid, return <tt/false/, otherwise return the relevant error string.<tag>self_print_js($ndx_array)</tag><p>Print out Javascript code to validate this element.<tt/$ndx_array/ is an array of the indices of the elements tobe validated as used in the Javascript form.element[] array. This isneeded since the extra <tt/[]/ in array valued element names confusesJavascript.<tag>self_load_defaults($val)</tag><p>Set the default value for this element to <tt/$val/. Usually thedefault definition of this function, which just copies <tt/$val/ to<tt/$this→value/ is all that is needed, but there may be specialcases that need to do something else. See the implementation of thecheckbox element for an example.</descrip>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -