[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/validation/src/ -> lmbErrorList.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/core/src/lmbCollection.class.php');
  10  lmb_require('limb/validation/src/lmbErrorMessage.class.php');
  11  lmb_require('limb/core/src/lmbObject.class.php');
  12  
  13  /**
  14   * Holds a list of validation errors
  15   * @see lmbErrorMessage
  16   * @package validation
  17   * @version $Id: lmbErrorList.class.php 5945 2007-06-06 08:31:43Z pachanga $
  18   */
  19  class lmbErrorList extends lmbCollection
  20  {
  21    /**

  22    * @see lmbErrorMessage :: getErrorMessage()

  23    * @see getFieldName()

  24    * @var object Field name dictionary that is used in making human readable error messages

  25    */
  26    protected $field_name_dictionary;
  27  
  28    /**

  29    * Sets new field name dictionary

  30    * Usually this happens in {@link WactFormComponent :: setErrors()}

  31    * @see WactFormFieldNameDictionary

  32    * @param mixed New field name dictionary object.

  33    * @return void

  34    */
  35    function setFieldNameDictionary($dictionary)
  36    {
  37      $this->field_name_dictionary = $dictionary;
  38    }
  39  
  40    /**

  41    * Adds new error.

  42    * Creates an object of {@link lmbErrorMessage} class.

  43    * Accepts error message, array of fields list which this error is belong to and array of values.

  44    * Error message can contain placeholders like {Placeholder} that will be replaced with field names

  45    * and values in {@link lmbErrorMessage :: getReadableErrorList()}

  46    * Here is an example of adding error to error list in some validation rule:

  47    * <code>

  48    *  $error_list->addError('{Field} must contain at least {min} characters.', array('Field' => 'password'), array('min' => 5));

  49    * </code>

  50    * After all replacements we can get something like "Password must contain at least 5 characters", there "password" becomes "Password"

  51    * @param string Error message with placeholders like {Field} must contain at least {min} characters.

  52    * @param array Array of aliases and field names like array('BaseField' => 'password', 'RepeatField' => 'repeat_password')

  53    * @param array Array of aliases and field values like array('Min' => 5, 'Max' => 15)

  54    * @return lmbErrorMessage

  55    */
  56    function addError($message, $fields = array(), $values = array())
  57    {
  58      $error = new lmbObject(array('message' => $message,
  59                                   'error' => $message, // for BC
  60                                   'fields' => $fields,
  61                                   'values' => $values));
  62      $this->add($error);
  63      return $error;
  64    }
  65  
  66    /**

  67    * Returns humal readable name of a field

  68    * Actually delegates to field name dictionary

  69    * @param string

  70    * @return string

  71    */
  72    function getFieldName($field)
  73    {
  74      if(is_object($this->field_name_dictionary))
  75        return $this->field_name_dictionary->getFieldName($field);
  76      else
  77        return $field;
  78    }
  79  
  80    /**

  81    * Returns FALSE is contains at least one error, otherwise returns TRUE

  82    * @return boolean

  83    */
  84    function isValid()
  85    {
  86      return $this->isEmpty();
  87    }
  88  
  89    /**

  90    * Return processed error list with translated and formatted messages

  91    * @see lmbErrorList :: addError()

  92    * @see __construct

  93    * @return string

  94    */
  95    function getReadable()
  96    {
  97      $new_list = new lmbErrorList();
  98  
  99      foreach($this as $error)
 100      {
 101        $text = $error->getMessage();
 102  
 103        foreach($error->getFields() as $key => $fieldName)
 104        {
 105          $replacement = '"' . $this->getFieldName($fieldName) . '"';
 106          $text = str_replace('{' . $key . '}', $replacement, $text);
 107        }
 108  
 109        foreach($error->getValues() as $key => $replacement)
 110          $text = str_replace('{' . $key . '}', $replacement, $text);
 111  
 112        $new_list->addError($text, $error->getFields(), $error->getValues());
 113      }
 114      return $new_list;
 115    }
 116  }


Generated: Sat Sep 6 04:46:52 2008 Cross-referenced by PHPXref 0.7