Revision [8279]

This is an old revision of AdvancedFormOpen made by JavaWoman on 2005-05-18 11:38:47.

 

Advanced Form Open


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:
  1.     /**
  2.      * Build an opening form tag with specified or generated attributes.
  3.      *
  4.      * This method builds an opening form tag, taking care that the result is valid XHTML
  5.      * no matter where the parameters come from: invalid parameters are ignored and defaults used.
  6.      * This enables this method to be used with user-provided parameter values.
  7.      *
  8.      * The form will always have the required action attribute and an id attribute to provide
  9.      * a 'hook' for styling and scripting. This method tries its best to ensure the id attribute
  10.      * is unique, among other things by adding a 'form_' prefix to make it different from ids for
  11.      * other elements.
  12.      * For a file upload form ($file=TRUE) the appropriate method and enctype attributes are generated.
  13.      *
  14.      * When rewriting is not active, a hidden field is attached as well to pass on the page name.
  15.      * NOTE: is this really needed??
  16.      *
  17.      * @author      {@link http://wikka.jsnx.com/JavaWoman JavaWoman}
  18.      * @copyright   Copyright © 2005, Marjolein Katsma
  19.      * @license     http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  20.      *
  21.      * @access  public
  22.      * @uses    existsHandler()
  23.      * @uses    existsPage()
  24.      * @uses    Href()
  25.      * @uses    MiniHref()
  26.      * @uses    ID_LENGTH
  27.      *
  28.      * @param   string  $method     optional: "method" which consists of handler and possibly a query string
  29.      *                              to be used as part of action attribute
  30.      * @param   string  $tag        optional: page name to be used for action attribute;
  31.      *                              if not specified, the current page will be used
  32.      * @param   string  $formMethod optional: method attribute; must be POST (default) or GET;
  33.      *                              anything but POST is ignored and considered as GET
  34.      * @param   string  $id         optional: id attribute
  35.      * @param   string  $class      optional: class attribute
  36.      * @param   boolean $file       optional: specifies whether there will be a file upload field;
  37.      *                              default: FALSE; if TRUE sets method attribute to POST and generates
  38.      *                              appropriate enctype attribute
  39.      * @return  string opening form tag and hidden input field when not rewriting.
  40.      */
  41.     function FormOpen($method='',$tag='',$formMethod='POST',$id='',$class='',$file=FALSE)
  42.     {
  43.         // initializations
  44.         static $seq = 1;
  45.         static $aIds = array();
  46.         $attrMethod = '';                                               # no method for HTML default GET
  47.         $attrClass = '';
  48.         $attrEnctype = '';                                              # default no enctype -> HTML default application/x-www-form-urlencoded
  49.         $hiddenval = '';
  50.         // validations
  51.         $validMethod = $this->existsHandler($method);
  52.         $validPage = $this->existsPage($tag);
  53.         $validId = preg_match('/^[A-Za-z][A-Za-z0-9_:.-]*$/',$id);      # http://www.w3.org/TR/html4/types.html#type-id
  54.         // derivations (MiniHref supplies current page name if none specified)
  55.         $page = ($validPage) ? $tag : '';
  56.         $method = ($validMethod) ? $method : '';
  57.  
  58.         // form action (action is a required attribute!)
  59.         $attrAction = ' action="'.$this->Href($method, $page).'"';
  60.         // form method (ignore anything but POST) and enctype
  61.         if (TRUE === $file)
  62.         {
  63.             $attrMethod = ' method="POST"';                             # required for file upload
  64.             $attrEnctype = ' enctype="multipart/form-data"';            # required for file upload
  65.         }
  66.         elseif (preg_match('/^POST$/i',$formMethod))                    # ignore case...
  67.         {
  68.             $attrMethod = ' method="POST"';                             # ...but generate uppercase
  69.         }
  70.         // form id
  71.         if ('' == $id || !$validId || in_array($id,$aIds))              # ignore specified id if it is invalid or exists already
  72.         {
  73.             $id = substr(md5($method.$tag.$formMethod.$class),0,ID_LENGTH); # @@@ maybe make length configurable
  74.             if (in_array($id,$aIds))
  75.             {
  76.                 $id .= '_'.++$seq;                                      # add suffiX to make ID unique
  77.             }
  78.             $attrId = ' id="form_'.$id.'"';
  79.         }
  80.         else
  81.         {
  82.             $attrId = ' id="form_'.$id.'"';
  83.         }
  84.         $aIds[] = $id;                                                  # keep track of both specified and generated ids!
  85.         // form class
  86.         if ('' != $class)
  87.         {
  88.             $attrClass = ' class="'.$class.'"';
  89.         }
  90.  
  91.         // build HTML fragment
  92.         $result = '<form'.$attrAction.$attrMethod.$attrEnctype.$attrId.$attrClass.'>'."\n";
  93.         if (!$this->config['rewrite_mode'])                             # @@@ is this bit really necessary?
  94.         {
  95.             $hiddenval = $this->MiniHref($method, $page);
  96.             $result .= '<fieldset class="hidden"><input type="hidden" name="wakka" value="'.$hiddenval.'" /></fieldset>'."\n";
  97.         }
  98.  
  99.         return $result;
  100.     }
There is one comment on this page. [Display comment]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki