Revision [8279]
This is an old revision of AdvancedFormOpen made by JavaWoman on 2005-05-18 11:38:47.
Advanced Form Open
See also:
The WikkaCore Wikka core has a FormOpen() method that creates the opening tag for a form. However it has a number of limitations, such as no way to specify an id and/or class attribute, and not supporting enctype needed for a file upload form. This leads to ugly workarounds and inconsitent (and conceivably invalid) code.The following replacement for FormOpen() addresses these issues and makes sure the generated code is valid XHTML. It uses a number of new supporting methods that may have more general usefulness as well.
New FormOpen() method
The folowing code should replace the FormOpen() method in wikka.php (at line 694 in the 1.1.6.0. release version:
- /**
- * Build an opening form tag with specified or generated attributes.
- *
- * This method builds an opening form tag, taking care that the result is valid XHTML
- * no matter where the parameters come from: invalid parameters are ignored and defaults used.
- * This enables this method to be used with user-provided parameter values.
- *
- * The form will always have the required action attribute and an id attribute to provide
- * a 'hook' for styling and scripting. This method tries its best to ensure the id attribute
- * is unique, among other things by adding a 'form_' prefix to make it different from ids for
- * other elements.
- * For a file upload form ($file=TRUE) the appropriate method and enctype attributes are generated.
- *
- * When rewriting is not active, a hidden field is attached as well to pass on the page name.
- * NOTE: is this really needed??
- *
- * @author {@link http://wikka.jsnx.com/JavaWoman JavaWoman}
- * @copyright Copyright © 2005, Marjolein Katsma
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
- *
- * @access public
- * @uses existsHandler()
- * @uses existsPage()
- * @uses Href()
- * @uses MiniHref()
- * @uses ID_LENGTH
- *
- * @param string $method optional: "method" which consists of handler and possibly a query string
- * to be used as part of action attribute
- * @param string $tag optional: page name to be used for action attribute;
- * if not specified, the current page will be used
- * @param string $formMethod optional: method attribute; must be POST (default) or GET;
- * anything but POST is ignored and considered as GET
- * @param string $id optional: id attribute
- * @param string $class optional: class attribute
- * @param boolean $file optional: specifies whether there will be a file upload field;
- * default: FALSE; if TRUE sets method attribute to POST and generates
- * appropriate enctype attribute
- * @return string opening form tag and hidden input field when not rewriting.
- */
- function FormOpen($method='',$tag='',$formMethod='POST',$id='',$class='',$file=FALSE)
- {
- // initializations
- static $seq = 1;
- $attrMethod = ''; # no method for HTML default GET
- $attrClass = '';
- $attrEnctype = ''; # default no enctype -> HTML default application/x-www-form-urlencoded
- $hiddenval = '';
- // validations
- $validMethod = $this->existsHandler($method);
- $validPage = $this->existsPage($tag);
- $validId = preg_match('/^[A-Za-z][A-Za-z0-9_:.-]*$/',$id); # http://www.w3.org/TR/html4/types.html#type-id
- // derivations (MiniHref supplies current page name if none specified)
- $page = ($validPage) ? $tag : '';
- $method = ($validMethod) ? $method : '';
- // form action (action is a required attribute!)
- $attrAction = ' action="'.$this->Href($method, $page).'"';
- // form method (ignore anything but POST) and enctype
- if (TRUE === $file)
- {
- $attrMethod = ' method="POST"'; # required for file upload
- $attrEnctype = ' enctype="multipart/form-data"'; # required for file upload
- }
- {
- $attrMethod = ' method="POST"'; # ...but generate uppercase
- }
- // form id
- if ('' == $id || !$validId || in_array($id,$aIds)) # ignore specified id if it is invalid or exists already
- {
- {
- $id .= '_'.++$seq; # add suffiX to make ID unique
- }
- $attrId = ' id="form_'.$id.'"';
- }
- else
- {
- $attrId = ' id="form_'.$id.'"';
- }
- $aIds[] = $id; # keep track of both specified and generated ids!
- // form class
- if ('' != $class)
- {
- $attrClass = ' class="'.$class.'"';
- }
- // build HTML fragment
- $result = '<form'.$attrAction.$attrMethod.$attrEnctype.$attrId.$attrClass.'>'."\n";
- if (!$this->config['rewrite_mode']) # @@@ is this bit really necessary?
- {
- $hiddenval = $this->MiniHref($method, $page);
- $result .= '<fieldset class="hidden"><input type="hidden" name="wakka" value="'.$hiddenval.'" /></fieldset>'."\n";
- }
- return $result;
- }