[ Index ]

PHP Cross Reference of Web Application Component Toolkit

title

Body

[close]

/framework/view/ -> formview.inc.php (source)

   1  <?php
   2  //--------------------------------------------------------------------------------
   3  // Copyright 2003 Procata, Inc.
   4  // Released under the LGPL license (http://www.gnu.org/copyleft/lesser.html)
   5  //--------------------------------------------------------------------------------
   6  /**
   7  * @package WACT_CONTROLLERS
   8  * @version $Id: formview.inc.php,v 1.10 2004/11/12 21:25:12 jeffmoore Exp $
   9  */
  10  //--------------------------------------------------------------------------------
  11  /**
  12  * Include the Template class
  13  */
  14  require_once WACT_ROOT . 'view/view.inc.php';
  15  
  16  /**
  17  * Implements TemplateView pattern for Forms.
  18  * @see http://wact.sourceforge.net/index.php/FormView
  19  * @access public
  20  */
  21  class FormView extends View {
  22  
  23      /**
  24      * Instance of FormComponent found in template
  25      * @var FormComponent
  26      * @access public
  27      */    
  28      var $Form;
  29      
  30      /**
  31      * Preserved fields allow the controller to tell the view to
  32      * preserve the state of certain fields between requests.
  33      * In a form view, this usually means add hidden fields.
  34      * @param string template filename
  35      * @param array form fields to preserve
  36      * @access public
  37      */
  38      function FormView($TemplateFile = NULL, $PreservedFields = array()) {
  39          parent::View($TemplateFile);
  40          if (!is_null($TemplateFile)) {
  41              $this->findForm();
  42          }
  43          $this->preserveState($PreservedFields);
  44      }
  45      
  46      /**
  47      * This only works for templates that contain a single form.
  48      * @return void
  49      * @access protected
  50      */
  51      function findForm() {
  52          $this->Form =& $this->Template->findChildByClass('FormComponent');
  53          // Need to raise an error here if the form component is not found.
  54      }
  55  
  56      /**
  57      * Turn this View into a sub view in a composite view
  58      * The root component passed represents the portion of the
  59      * CompositeView's Template that this view will manage.
  60      * This component should be a dataspace component unless
  61      * special arrangements are made in subclasses of this
  62      * View class.
  63      * @param object 
  64      */
  65      function makeSubView(&$RootComponent) {
  66          parent::makeSubView($RootComponent);
  67          $this->findForm();
  68      }
  69      
  70      /**
  71      * Preserve the state of a list of fields.  
  72      * Presumably by creating hidden fields in the form.
  73      * @param array of field to preserve
  74      * @return void
  75      * @access private
  76      */
  77      function preserveState($PreservedFields) {
  78          foreach ($PreservedFields as $Field) {
  79              $this->Form->preserveState($Field);
  80          }
  81      }
  82  
  83      /**
  84      * Transfer values from the dataspace into the form
  85      * @param object implementing dataspace
  86      * @return void
  87      * @access public
  88      */
  89      function setModel(&$Model) {
  90          $this->Form->registerDataSource($Model);
  91      }
  92      
  93      /**
  94      * Transfer a list of errors into the form.
  95      * @param Validator instance of validator or subclass
  96      * @return void
  97      * @access private
  98      */
  99      function setErrors(&$Validator) {
 100          $this->Form->setErrors($Validator->getErrorList());
 101      }
 102      
 103      /**
 104      * Called only on the request when the form is first displayed
 105      * This template method offers the opportunity to perform stage
 106      * specific presentation logic.
 107      * @access public
 108      * @return void
 109      * @abstract
 110      */
 111      function onInitial() {
 112      }
 113  
 114      /**
 115      * Called only on requests after the second one.  May never be called.
 116      * This template method offers the opportunity to perform stage
 117      * specific presentation logic.
 118      * @access public
 119      * @return void    
 120      * @abstract
 121      */
 122      function onSubmitted() {
 123      }
 124      
 125      /**
 126      * Can this view do client side validation?
 127      * @return boolean
 128      * @access protected
 129      */
 130      function canClientValidate() {
 131          return TRUE;
 132      }
 133      
 134      /**
 135      * Client side validation population. This implementation sets the maxlength
 136      * attribute of input text fields. This method might be overridden for JavaScript
 137      * validation, for example.
 138      * Transfer information from the validator to the view.
 139      * @param Validator or subclass
 140      * @return void
 141      * @access protected
 142      */
 143      function setClientValidation(&$Validator) {
 144          foreach( array_keys($Validator->rules) as $key) {
 145              $Rule =& $Validator->rules[$key];
 146              if (is_a($Rule, 'SizeRangeRule')) {
 147                  $Field =& $this->Form->findChild($Rule->fieldname);
 148                  if ($Field && is_a($Field, 'InputTextComponent')) {
 149                      $Field->setAttribute('maxlength', $Rule->maxLength);
 150                  }
 151              }
 152          }
 153      }
 154  }
 155  ?>


Generated: Sun Nov 28 19:36:09 2004 Cross-referenced by PHPXref 0.5