HTML_QuickForm2
[ class tree: HTML_QuickForm2 ] [ index: HTML_QuickForm2 ] [ all elements ]

Source for file QuickForm2.php

Documentation is available at QuickForm2.php

  1. <?php
  2. /**
  3.  * Class representing a HTML form
  4.  *
  5.  * PHP version 5
  6.  *
  7.  * LICENSE:
  8.  * 
  9.  * Copyright (c) 2006, 2007, Alexey Borzov <avb@php.net>,
  10.  *                           Bertrand Mansion <golgote@mamasam.com>
  11.  * All rights reserved.
  12.  *
  13.  * Redistribution and use in source and binary forms, with or without
  14.  * modification, are permitted provided that the following conditions
  15.  * are met:
  16.  *
  17.  *    * Redistributions of source code must retain the above copyright
  18.  *      notice, this list of conditions and the following disclaimer.
  19.  *    * Redistributions in binary form must reproduce the above copyright
  20.  *      notice, this list of conditions and the following disclaimer in the
  21.  *      documentation and/or other materials provided with the distribution.
  22.  *    * The names of the authors may not be used to endorse or promote products
  23.  *      derived from this software without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  33.  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36.  *
  37.  * @category   HTML
  38.  * @package    HTML_QuickForm2
  39.  * @author     Alexey Borzov <avb@php.net>
  40.  * @author     Bertrand Mansion <golgote@mamasam.com>
  41.  * @license    http://opensource.org/licenses/bsd-license.php New BSD License
  42.  * @version    CVS: $Id: QuickForm2.php,v 1.6 2007/05/29 20:40:10 avb Exp $
  43.  * @link       http://pear.php.net/package/HTML_QuickForm2
  44.  */
  45.  
  46. /**
  47.  * Abstract base class for QuickForm2 containers
  48.  */
  49. require_once 'HTML/QuickForm2/Container.php';
  50.  
  51. /**
  52.  * Data source for HTML_QuickForm2 objects based on superglobal arrays
  53.  */
  54. require_once 'HTML/QuickForm2/DataSource/SuperGlobal.php';
  55.  
  56. /**
  57.  * Class representing a HTML form
  58.  *
  59.  * @category   HTML
  60.  * @package    HTML_QuickForm2
  61.  * @author     Alexey Borzov <avb@php.net>
  62.  * @author     Bertrand Mansion <golgote@mamasam.com>
  63.  * @version    Release: 0.2.0
  64.  */
  65. {
  66.    /**
  67.     * Data sources providing values for form elements
  68.     * @var array 
  69.     */
  70.     protected $datasources = array();
  71.  
  72.    /**
  73.     * We do not allow setting "method" and "id" other than through constructor
  74.     * @var array 
  75.     */
  76.     protected $watchedAttributes = array('id''method');
  77.  
  78.    /**
  79.     * Class constructor, form's "id" and "method" attributes can only be set here
  80.     *
  81.     * @param    string  "id" attribute of <form> tag
  82.     * @param    string  HTTP method used to submit the form
  83.     * @param    mixed   Additional attributes (either a string or an array)
  84.     * @param    bool    Whether to track if the form was submitted by adding
  85.     *                    a special hidden field
  86.     */ 
  87.     public function __construct($id$method 'post'$attributes = null$trackSubmit = true)
  88.     {
  89.         $method ('GET' == strtoupper($method))'get''post';
  90.         if (empty($id)) {
  91.             $id          = self::generateId('');
  92.             $trackSubmit = false;
  93.         else {
  94.             self::storeId($id);
  95.         }
  96.         $this->attributes array_merge(
  97.                                 self::prepareAttributes($attributes),
  98.                                 array('id' => (string)$id'method' => $method)
  99.                             );
  100.         if (!isset($this->attributes['action'])) {
  101.             $this->attributes['action'$_SERVER['PHP_SELF'];
  102.         }
  103.         if ($trackSubmit && isset($_REQUEST['_qf__' $id]||
  104.             !$trackSubmit && ('get' == $method && !empty($_GET||
  105.                               'post' == $method && (!empty($_POST|| !empty($_FILES))))
  106.         {
  107.             $this->addDataSource(new HTML_QuickForm2_DataSource_SuperGlobal(
  108.                 $methodget_magic_quotes_gpc()
  109.             ));
  110.         }
  111.         if ($trackSubmit{
  112.             $this->appendChild(HTML_QuickForm2_Factory::createElement(
  113.                 'hidden''_qf__' $id
  114.             ));
  115.         }
  116.     }
  117.  
  118.     protected function onAttributeChange($name$value = null)
  119.     {
  120.         throw new HTML_QuickForm2_InvalidArgumentException(
  121.             'Attribute \'' $name '\' is read-only'
  122.         );
  123.     }
  124.  
  125.     protected function setContainer(HTML_QuickForm2_Container $container = null)
  126.     {
  127.         throw new HTML_QuickForm2_Exception('Form cannot be added to container');
  128.     }
  129.  
  130.     public function setId($id = null)
  131.     {
  132.         throw new HTML_QuickForm2_InvalidArgumentException(
  133.             "Attribute 'id' is read-only"
  134.         );
  135.     }
  136.  
  137.  
  138.    /**
  139.     * Adds a new data source to the form
  140.     *
  141.     * @param    HTML_QuickForm2_DataSource  Data source
  142.     */
  143.     public function addDataSource(HTML_QuickForm2_DataSource $datasource)
  144.     {
  145.         $this->datasources[$datasource;
  146.         $this->updateValue();
  147.     }
  148.  
  149.    /**
  150.     * Replaces the list of form's data sources with a completely new one
  151.     *
  152.     * @param    array   A new data source list
  153.     * @throws   HTML_QuickForm2_InvalidArgumentException    if given array
  154.     *                contains something that is not a valid data source
  155.     */
  156.     public function setDataSources(array $datasources)
  157.     {
  158.         foreach ($datasources as $ds{
  159.             if (!$ds instanceof HTML_QuickForm2_DataSource{
  160.                 throw new HTML_QuickForm2_InvalidArgumentException(
  161.                     'Array should contain only DataSource instances'
  162.                 );
  163.             }
  164.         }
  165.         $this->datasources = $datasources;
  166.         $this->updateValue();
  167.     }
  168.  
  169.    /**
  170.     * Returns the list of data sources attached to the form
  171.     *
  172.     * @return   array 
  173.     */
  174.     public function getDataSources()
  175.     {
  176.         return $this->datasources;
  177.     }
  178.  
  179.     public function getType()
  180.     {
  181.         return 'form';
  182.     }
  183.  
  184.     public function setValue($value)
  185.     {
  186.         throw new HTML_QuickForm2_Exception('Not implemented');
  187.     }
  188.  
  189.     public function __toString()
  190.     {
  191.         throw new HTML_QuickForm2_Exception('Not implemented');
  192.     }
  193.  
  194.    /**
  195.     * Performs the server-side validation
  196.     *
  197.     * @return   boolean Whether all form's elements are valid
  198.     */
  199.     public function validate()
  200.     {
  201.         $isSubmitted = false;
  202.         foreach ($this->datasources as $ds{
  203.             if ($ds instanceof HTML_QuickForm2_DataSource_Submit{
  204.                 $isSubmitted = true;
  205.                 break;
  206.             }
  207.         }
  208.         return $isSubmitted? parent::validate(): false;
  209.     }
  210. }
  211. ?>

Documentation generated on Mon, 22 Oct 2007 12:30:22 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.