Development

Changeset 5816

You must first sign up to be able to contribute.

Changeset 5816

Show
Ignore:
Timestamp:
11/02/07 16:59:47 (10 months ago)
Author:
fabien
Message:

updated validators

  • better support for options
    • added a addRequiredOption() method
    • check option name for typos
  • check error codes for typos
  • added 2 methods to get default options and error messages
  • added support for date as an array to sfValidatorDate
  • fixed some minor bugs
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/validator/sfValidator.class.php

    r5753 r5816  
    2525 
    2626  protected 
    27     $messages = array(), 
    28     $options  = array(); 
     27    $requiredOptions = array(), 
     28    $defaultMessages = array(), 
     29    $defaultOptions  = array(), 
     30    $messages        = array(), 
     31    $options         = array(); 
    2932 
    3033  /** 
     
    4245  public function __construct($options = array(), $messages = array()) 
    4346  { 
    44     $this->messages = array('required' => 'Required.', 'invalid' => 'Invalid.'); 
    45     $this->options  = array('required' => true, 'trim' => false, 'empty_value' => null); 
     47    $this->options  = array_merge(array('required' => true, 'trim' => false, 'empty_value' => null), $this->options); 
     48    $this->messages = array_merge(array('required' => 'Required.', 'invalid' => 'Invalid.'), $this->messages); 
    4649 
    4750    $this->configure($options, $messages); 
     51 
     52    $this->setDefaultOptions($this->getOptions()); 
     53    $this->setDefaultMessages($this->getMessages()); 
     54 
     55    // check option names 
     56    if ($diff = array_diff(array_keys($options), array_merge(array_keys($this->options), $this->requiredOptions))) 
     57    { 
     58      throw new sfException(sprintf('%s does not support the following options: \'%s\'.', get_class($this), implode('\', \'', $diff))); 
     59    } 
     60 
     61    // check error code names 
     62    if ($diff = array_diff(array_keys($messages), array_keys($this->messages))) 
     63    { 
     64      throw new sfException(sprintf('%s does not support the following error codes: \'%s\'.', get_class($this), implode('\', \'', $diff))); 
     65    } 
     66 
     67    // check required options 
     68    if ($diff = array_diff($this->requiredOptions, array_keys($options))) 
     69    { 
     70      throw new sfException(sprintf('%s requires the following options: \'%s\'.', get_class($this), implode('\', \'', $diff))); 
     71    } 
    4872 
    4973    $this->options  = array_merge($this->options, $options); 
    5074    $this->messages = array_merge($this->messages, $messages); 
     75  } 
     76 
     77  /** 
     78   * Configures the current validator. 
     79   * 
     80   * This method allows each validator to add options and error messages 
     81   * during validator creation. 
     82   * 
     83   * If some options and messages are given in the sfValidator constructor 
     84   * they will take precedence over the options and messages you configure 
     85   * in this method. 
     86   * 
     87   * @param array An array of options 
     88   * @param array An array of error messages 
     89   * 
     90   * @see __construct() 
     91   */ 
     92  protected function configure($options = array(), $messages = array()) 
     93  { 
    5194  } 
    5295 
     
    199242 
    200243  /** 
     244   * Sets the charset to use when validating strings. 
     245   * 
     246   * @param string The charset 
     247   */ 
     248  static public function setCharset($charset) 
     249  { 
     250    self::$charset = $charset; 
     251  } 
     252 
     253  /** 
     254   * Returns the charset to use when validating strings. 
     255   * 
     256   * @return string The charset (default to UTF-8) 
     257   */ 
     258  static public function getCharset() 
     259  { 
     260    return self::$charset; 
     261  } 
     262 
     263  /** 
     264   * Returns true if the value is empty. 
     265   * 
     266   * @param  mixed   The input value 
     267   * 
     268   * @return Boolean true if the value is empty, false otherwise 
     269   */ 
     270  protected function isEmpty($value) 
     271  { 
     272    return in_array($value, array(null, '')); 
     273  } 
     274 
     275  /** 
     276   * Returns an empty value for this validator. 
     277   * 
     278   * @return mixed The empty value for this validator 
     279   */ 
     280  protected function getEmptyValue() 
     281  { 
     282    return $this->getOption('empty_value'); 
     283  } 
     284 
     285  /** 
    201286   * Returns an array of all error codes for this validator. 
    202287   * 
    203    * Subclasses of sfValidator may override this method to register 
    204    * their own error codes. 
    205    * 
    206    * By default this method return required and invalid as errors codes. 
    207    * 
    208288   * @return array An array of possible error codes 
    209    */ 
    210   public function getErrorCodes() 
    211   { 
    212     return array('required', 'invalid'); 
    213   } 
    214  
    215   /** 
    216    * Sets the charset to use when validating strings. 
    217    * 
    218    * @param string The charset 
    219    */ 
    220   static public function setCharset($charset) 
    221   { 
    222     self::$charset = $charset; 
    223   } 
    224  
    225   /** 
    226    * Returns the charset to use when validating strings. 
    227    * 
    228    * @return string The charset (default to UTF-8) 
    229    */ 
    230   static public function getCharset() 
    231   { 
    232     return self::$charset; 
    233   } 
    234  
    235   /** 
    236    * Returns true if the value is empty. 
    237    * 
    238    * @param  mixed   The input value 
    239    * 
    240    * @return Boolean true if the value is empty, false otherwise 
    241    */ 
    242   protected function isEmpty($value) 
    243   { 
    244     return in_array($value, array(null, '')); 
    245   } 
    246  
    247   /** 
    248    * Returns an empty value for this validator. 
    249    * 
    250    * @return mixed The empty value for this validator 
    251    */ 
    252   protected function getEmptyValue() 
    253   { 
    254     return $this->getOption('empty_value'); 
    255   } 
    256  
    257   /** 
    258    * Configures the current validator. 
    259    * 
    260    * This method allows each validator to add options and error messages 
    261    * during validator creation. 
    262    * 
    263    * If some options and messages are given in the sfValidator constructor 
    264    * they will take precedence over the options and messages you configure 
    265    * in this method. 
    266    * 
    267    * @param array An array of options 
    268    * @param array An array of error messages 
    269    * 
    270    * @see __construct() 
    271    */ 
    272   protected function configure($options = array(), $messages = array()) 
    273   { 
     289   * 
     290   * @see getDefaultMessages() 
     291   */ 
     292  final public function getErrorCodes() 
     293  { 
     294    return array_keys($this->getDefaultMessages()); 
     295  } 
     296 
     297  /** 
     298   * Returns default messages for all possible error codes. 
     299   * 
     300   * @return array An array of default error codes and messages 
     301   */ 
     302  public function getDefaultMessages() 
     303  { 
     304    return $this->defaultMessages; 
     305  } 
     306 
     307  /** 
     308   * Sets default messages for all possible error codes. 
     309   * 
     310   * @param array An array of default error codes and messages 
     311   */ 
     312  protected function setDefaultMessages($messages) 
     313  { 
     314    $this->defaultMessages = $messages; 
     315  } 
     316 
     317  /** 
     318   * Returns default option values. 
     319   * 
     320   * @return array An array of default option values 
     321   */ 
     322  public function getDefaultOptions() 
     323  { 
     324    return $this->defaultOptions; 
     325  } 
     326 
     327  /** 
     328   * Sets default option values. 
     329   * 
     330   * @param array An array of default option values 
     331   */ 
     332  protected function setDefaultOptions($options) 
     333  { 
     334    $this->defaultOptions = $options; 
     335  } 
     336 
     337  /** 
     338   * Adds a required option. 
     339   * 
     340   * @param string The option name 
     341   */ 
     342  public function addRequiredOption($name) 
     343  { 
     344    $this->requiredOptions[] = $name; 
     345  } 
     346 
     347  /** 
     348   * Returns all required option names. 
     349   * 
     350   * @param array An array of required option names 
     351   */ 
     352  public function getRequiredOptions() 
     353  { 
     354    return $this->requiredOptions; 
    274355  } 
    275356 
     
    295376 
    296377  /** 
     378   * Returns all error messages with non default values. 
     379   * 
     380   * @return string A string representation of the error messages 
     381   */ 
     382  protected function getMessagesWithoutDefaults() 
     383  { 
     384    $messages = $this->messages; 
     385 
     386    // remove default option values 
     387    foreach ($this->getDefaultMessages() as $key => $value) 
     388    { 
     389      if (array_key_exists($key, $messages) && $messages[$key] === $value) 
     390      { 
     391        unset($messages[$key]); 
     392      } 
     393    } 
     394 
     395    return $messages; 
     396  } 
     397 
     398  /** 
    297399   * Returns all options with non default values. 
    298400   * 
     
    304406 
    305407    // remove default option values 
    306     $reflection = new ReflectionClass(get_class($this)); 
    307     $default = $reflection->newInstanceArgs(func_get_args()); 
    308     foreach ($default->getOptions() as $key => $value) 
     408    foreach ($this->getDefaultOptions() as $key => $value) 
    309409    { 
    310410      if (array_key_exists($key, $options) && $options[$key] === $value) 
     
    316416    return $options; 
    317417  } 
    318  
    319   /** 
    320    * Returns all error messages with non default values. 
    321    * 
    322    * @return string A string representation of the error messages 
    323    */ 
    324   protected function getMessagesWithoutDefaults() 
    325   { 
    326     $messages = $this->messages; 
    327  
    328     // remove default option values 
    329     $reflection = new ReflectionClass(get_class($this)); 
    330     $default = $reflection->newInstanceArgs(func_get_args()); 
    331     foreach ($default->getMessages() as $key => $value) 
    332     { 
    333       if (array_key_exists($key, $messages) && $messages[$key] === $value) 
    334       { 
    335         unset($messages[$key]); 
    336       } 
    337     } 
    338  
    339     return $messages; 
    340   } 
    341418} 
  • trunk/lib/validator/sfValidatorAll.class.php

    r5753 r5816  
    5555    } 
    5656 
    57     if (!isset($messages['invalid'])) 
    58     { 
    59       $messages['invalid'] = null; 
    60     } 
     57    parent::__construct($options, $messages); 
     58  } 
    6159 
    62     parent::__construct($options, $messages); 
     60  /** 
     61   * @see sfValidator 
     62   */ 
     63  public function configure($options = array(), $messages = array()) 
     64  { 
     65    $this->setMessage('invalid', null); 
    6366  } 
    6467 
     
    145148    } 
    146149 
    147     return sprintf("%s(%s%s)", 
    148       str_repeat(' ', $indent), 
    149       $validators, 
    150       str_repeat(' ', $indent) 
    151     ); 
     150    return sprintf("%s(%s%s)", str_repeat(' ', $indent), $validators, str_repeat(' ', $indent)); 
    152151  } 
    153152} 
  • trunk/lib/validator/sfValidatorAny.class.php

    r5753 r5816  
    5555    } 
    5656 
    57     if (!isset($messages['invalid'])) 
    58     { 
    59       $messages['invalid'] = null; 
    60     } 
     57    parent::__construct($options, $messages); 
     58  } 
    6159 
    62     parent::__construct($options, $messages); 
     60  /** 
     61   * @see sfValidator 
     62   */ 
     63  public function configure($options = array(), $messages = array()) 
     64  { 
     65    $this->setMessage('invalid', null); 
    6366  } 
    6467 
     
    139142    } 
    140143 
    141     return sprintf("%s(%s%s)", 
    142       str_repeat(' ', $indent), 
    143       $validators, 
    144       str_repeat(' ', $indent) 
    145     ); 
     144    return sprintf("%s(%s%s)", str_repeat(' ', $indent), $validators, str_repeat(' ', $indent)); 
    146145  } 
    147146} 
  • trunk/lib/validator/sfValidatorBoolean.class.php

    r5634 r5816  
    3333    $this->setOption('true_values', array('true', 't', 'yes', 'y', 'on', '1')); 
    3434    $this->setOption('false_values', array('false', 'f', 'no', 'n', 'off', '0')); 
    35  
    3635    $this->setOption('required', false); 
    3736  } 
  • trunk/lib/validator/sfValidatorCallback.class.php

    r5753 r5816  
    2020{ 
    2121  /** 
    22    * Constructor. 
     22   * Configures the current validator. 
    2323   * 
    2424   * Available options: 
    2525   * 
    26    *  * callback: A valid PHP callback 
     26   *  * callback: A valid PHP callback (required) 
    2727   * 
    28    * @see sfValidator 
    29    */ 
    30   public function __construct($options = array(), $messages = array()) 
    31   { 
    32     if (!isset($options['callback'])) 
    33     { 
    34       throw new sfException('The "callback" option is mandatory.'); 
    35     } 
    36  
    37     parent::__construct($options, $messages); 
    38   } 
    39  
    40   /** 
    4128   * @see sfValidator 
    4229   */ 
    4330  protected function configure($options = array(), $messages = array()) 
    4431  { 
    45     $this->options['required'] = false; 
     32    $this->addRequiredOption('callback'); 
     33    $this->setOption('required', false); 
    4634  } 
    4735 
     
    5341    return call_user_func($this->getOption('callback'), $this, $value); 
    5442  } 
    55  
    56   /** 
    57    * @see sfValidator 
    58    */ 
    59   protected function getOptionsWithoutDefaults() 
    60   { 
    61     return parent::getOptionsWithoutDefaults(array('callback' => array('--fake--'))); 
    62   } 
    63  
    64   /** 
    65    * @see sfValidator 
    66    */ 
    67   protected function getMessagesWithoutDefaults() 
    68   { 
    69     return parent::getMessagesWithoutDefaults(array('callback' => array('--fake--'))); 
    70   } 
    7143} 
  • trunk/lib/validator/sfValidatorChoice.class.php

    r5757 r5816  
    2020{ 
    2121  /** 
    22    * Constructor. 
     22   * Configures the current validator. 
    2323   * 
    2424   * Available options: 
    2525   * 
    26    *  * choices: An array of expected values 
     26   *  * choices: An array of expected values (required) 
    2727   * 
    2828   * @see sfValidator 
    2929   */ 
    30   public function __construct($options = array(), $messages = array()) 
     30  protected function configure($options = array(), $messages = array()) 
    3131  { 
    32     if (!isset($options['choices'])) 
    33     { 
    34       throw new sfException('The "choices" option is mandatory.'); 
    35     } 
    36  
    37     parent::__construct($options, $messages); 
     32    $this->addRequiredOption('choices'); 
    3833  } 
    3934 
     
    5045    return $value; 
    5146  } 
    52  
    53   /** 
    54    * @see sfValidator 
    55    */ 
    56   protected function getOptionsWithoutDefaults() 
    57   { 
    58     return parent::getOptionsWithoutDefaults(array('choices' => array('--fake--'))); 
    59   } 
    60  
    61   /** 
    62    * @see sfValidator 
    63    */ 
    64   protected function getMessagesWithoutDefaults() 
    65   { 
    66     return parent::getMessagesWithoutDefaults(array('choices' => array('--fake--'))); 
    67   } 
    6847} 
  • trunk/lib/validator/sfValidatorDate.class.php

    r5581 r5816  
    3535    $this->setMessage('bad_format', '"%value%" does not match the date format (%date_format%).'); 
    3636 
     37    $this->setOption('date_format', null); 
    3738    $this->setOption('with_time', false); 
    3839    $this->setOption('date_output', 'Y-m-d'); 
     
    4546  protected function doClean($value) 
    4647  { 
    47     if ($regex = $this->getOption('date_format')) 
     48    if (is_array($value)) 
     49    { 
     50      $clean = $this->convertDateArrayToTimestamp($value); 
     51    } 
     52    else if ($regex = $this->getOption('date_format')) 
    4853    { 
    4954      if (!preg_match($regex, $value, $match)) 
     
    5257      } 
    5358 
    54       if ($this->getOption('with_time')) 
    55       { 
    56         $clean = mktime(isset($match['hour']) ? $match['hour'] : 0, isset($match['minute']) ? $match['minute'] : 0, isset($match['second']) ? $match['second'] : 0, $match['month'], $match['day'], $match['year']); 
    57       } 
    58       else 
    59       { 
    60         $clean = mktime(0, 0, 0, $match['month'], $match['day'], $match['year']); 
    61       } 
     59      $clean = $this->convertDateArrayToTimestamp($match); 
    6260    } 
    6361    else if (!ctype_digit($value)) 
    6462    { 
    6563      $clean = strtotime($value); 
    66       if ($clean === -1 || $clean === false
     64      if (false === $clean
    6765      { 
    6866        throw new sfValidatorError($this, 'invalid', array('value' => $value)); 
     
    7876 
    7977  /** 
    80    * @see sfValidator 
     78   * Converts an array representing a date to a timestamp. 
     79   * 
     80   * The array can contains the following keys: year, month, day, hour, minute, second 
     81   * 
     82   * @param  array   An array of date elements 
     83   * 
     84   * @return integer A timestamp 
    8185   */ 
    82   public function getErrorCodes(
     86  protected function convertDateArrayToTimestamp($value
    8387  { 
    84     return array_merge(parent::getErrorCodes(), array('date_format')); 
     88    if ($this->getOption('with_time')) 
     89    { 
     90      $clean = mktime(isset($value['hour']) ? $value['hour'] : 0, isset($value['minute']) ? $value['minute'] : 0, isset($value['second']) ? $value['second'] : 0, $value['month'], $value['day'], $value['year']); 
     91    } 
     92    else 
     93    { 
     94      $clean = mktime(0, 0, 0, $value['month'], $value['day'], $value['year']); 
     95    } 
     96 
     97    if (false === $clean) 
     98    { 
     99      throw new sfValidatorError($this, 'invalid', array('value' => var_export($value, true))); 
     100    } 
     101 
     102    return $clean; 
    85103  } 
    86104} 
  • trunk/lib/validator/sfValidatorDecorator.class.php

    r5753 r5816  
    149149   * @see sfValidator 
    150150   */ 
    151   public function getErrorCodes(
     151  public function asString($indent = 0
    152152  { 
    153     return $this->getValidator()->getErrorCodes(); 
     153    return $this->validator->asString($indent); 
    154154  } 
    155155 
     
    157157   * @see sfValidator 
    158158   */ 
    159   public function asString($indent = 0
     159  public function getDefaultOptions(
    160160  { 
    161     return $this->getValidator()->asString($indent); 
     161    return $this->validator->getDefaultOptions(); 
     162  } 
     163 
     164  /** 
     165   * @see sfValidator 
     166   */ 
     167  public function getDefaultMessages() 
     168  { 
     169    return $this->validator->getDefaultMessages(); 
    162170  } 
    163171} 
  • trunk/lib/validator/sfValidatorEmail.class.php

    r5753 r5816  
    2222   * @see sfValidatorRegex 
    2323   */ 
    24   public function __construct($options = array(), $messages = array()) 
     24  protected function configure($options = array(), $messages = array()) 
    2525  { 
    26     $options['pattern'] = '/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i'; 
    27  
    28     parent::__construct($options, $messages); 
     26    $this->setOption('pattern', '/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i'); 
    2927  } 
    3028} 
  • trunk/lib/validator/sfValidatorInteger.class.php

    r5633 r5816  
    3434    $this->setMessage('min', '"%value%" must be greater than %min%.'); 
    3535    $this->setMessage('invalid', '"%value%" is not an integer.'); 
     36 
     37    $this->setOption('min', null); 
     38    $this->setOption('max', null); 
    3639  } 
    3740 
     
    6063    return $clean; 
    6164  } 
    62  
    63   /** 
    64    * @see sfValidator 
    65    */ 
    66   public function getErrorCodes() 
    67   { 
    68     return array_merge(parent::getErrorCodes(), array('max', 'min')); 
    69   } 
    7065} 
  • trunk/lib/validator/sfValidatorNumber.class.php

    r5581 r5816  
    3434    $this->setMessage('min', '"%value%" is too short (smallest allowed is %min%).'); 
    3535    $this->setMessage('invalid', '"%value%" is not a number.'); 
     36 
     37    $this->setOption('min', null); 
     38    $this->setOption('max', null); 
    3639  } 
    3740 
     
    6063    return $clean; 
    6164  } 
    62  
    63   /** 
    64    * @see sfValidator 
    65    */ 
    66   public function getErrorCodes() 
    67   { 
    68     return array_merge(parent::getErrorCodes(), array('max', 'min')); 
    69   } 
    7065} 
  • trunk/lib/validator/sfValidatorRegex.class.php

    r5753 r5816  
    2020{ 
    2121  /** 
    22    * Constructor. 
     22   * Configures the current validator. 
    2323   * 
    2424   * Available options: 
    2525   * 
    26    *  * pattern: A regex pattern compatible with PCRE 
     26   *  * pattern: A regex pattern compatible with PCRE (required) 
    2727   * 
    2828   * @see sfValidator 
    2929   */ 
    30   public function __construct($options = array(), $messages = array()) 
     30  protected function configure($options = array(), $messages = array()) 
    3131  { 
    32     if (!isset($options['pattern'])) 
    33     { 
    34       throw new sfException('The "pattern" option is mandatory.'); 
    35     } 
    36  
    37     parent::__construct($options, $messages); 
     32    $this->addRequiredOption('pattern'); 
    3833  } 
    3934 
     
    5247    return $clean; 
    5348  } 
    54  
    55   /** 
    56    * @see sfValidator 
    57    */ 
    58   protected function getOptionsWithoutDefaults() 
    59   { 
    60     return parent::getOptionsWithoutDefaults(array('pattern' => array('--fake--'))); 
    61   } 
    62  
    63   /** 
    64    * @see sfValidator 
    65    */ 
    66   protected function getMessagesWithoutDefaults() 
    67   { 
    68     return parent::getMessagesWithoutDefaults(array('pattern' => array('--fake--'))); 
    69   } 
    7049} 
  • trunk/lib/validator/sfValidatorSchema.class.php

    r5753 r5816  
    7070  public function configure($options = array(), $messages = array()) 
    7171  { 
    72     $this->options['allow_extra_fields']  = false
    73     $this->options['filter_extra_fields'] = true
    74  
    75     $this->messages['extra_fields'] = 'Extra field %field%.'
     72    $this->setOption('allow_extra_fields', false)
     73    $this->setOption('filter_extra_fields', true)
     74 
     75    $this->setMessage('extra_fields', 'Extra field %field%.')
    7676  } 
    7777 
     
    190190 
    191191  /** 
    192    * @see sfValidator 
    193    */ 
    194   public function getErrorCodes() 
    195   { 
    196     return array_merge(parent::getErrorCodes(), array('extra_fields')); 
    197   } 
    198  
    199   /** 
    200192   * Returns true if the schema has a field with the given name (implements the ArrayAccess interface). 
    201193   * 
  • trunk/lib/validator/sfValidatorSchemaCompare.class.php

    r5753 r5816  
    4848  public function __construct($leftField, $operator, $rightField, $options = array(), $messages = array()) 
    4949  { 
    50     $options['leftField']  = $leftField
    51     $options['operator']   = $operator
    52     $options['rightField'] = $rightField
     50    $this->setOption('left_field', $leftField)
     51    $this->setOption('operator', $operator)
     52    $this->setOption('right_field', $rightField)
    5353 
    5454    parent::__construct(null, $options, $messages); 
    55   } 
    56  
    57   /** 
    58    * @see sfValidator 
    59    */ 
    60   public function clean($value) 
    61   { 
    62     return $this->doClean($value); 
    6355  } 
    6456 
     
    7870    } 
    7971 
    80     $leftValue  = isset($values[$this->getOption('leftField')]) ? $values[$this->getOption('leftField')] : null; 
    81     $rightValue = isset($values[$this->getOption('rightField')]) ? $values[$this->getOption('rightField')] : null; 
     72    $leftValue  = isset($values[$this->getOption('left_field')]) ? $values[$this->getOption('left_field')] : null; 
     73    $rightValue = isset($values[$this->getOption('right_field')]) ? $values[$this->getOption('right_field')] : null; 
    8274 
    8375    switch ($this->getOption('operator')) 
     
    120112  public function asString($indent = 0) 
    121113  { 
    122     $options = $this->getOptionsWithoutDefaults('--fake--', '--', '--fake--'); 
    123     $messages = $this->getMessagesWithoutDefaults('--fake--', '--', '--fake--'); 
    124     unset($options['leftField'], $options['operator'], $options['rightField']); 
     114    $options = $this->getOptionsWithoutDefaults(); 
     115    $messages = $this->getMessagesWithoutDefaults(); 
     116    unset($options['left_field'], $options['operator'], $options['right_field']); 
    125117 
    126118    $arguments = ''; 
     
    135127    return sprintf('%s%s %s%s %s', 
    136128      str_repeat(' ', $indent), 
    137       $this->getOption('leftField'), 
     129      $this->getOption('left_field'), 
    138130      $this->getOption('operator'), 
    139131      $arguments, 
    140       $this->getOption('rightField') 
     132      $this->getOption('right_field') 
    141133    ); 
    142134  } 
  • trunk/lib/validator/sfValidatorSchemaFilter.class.php

    r5753 r5816  
    3131  public function __construct($field, sfValidator $validator, $options = array(), $messages = array()) 
    3232  { 
    33     $options['field']     = $field
    34     $options['validator'] = $validator
     33    $this->setOption('field', $field)
     34    $this->setOption('validator', $validator)
    3535 
    3636    parent::__construct(null, $options, $messages); 
    37   } 
    38  
    39   /** 
    40    * @see sfValidator 
    41    */ 
    42   public function clean($value) 
    43   { 
    44     return $this->doClean($value); 
    4537  } 
    4638 
  • trunk/lib/validator/sfValidatorString.class.php

    r5581 r5816  
    3434    $this->setMessage('min_length', '"%value%" is too short (%min_length% characters min).'); 
    3535 
     36    $this->setOption('max_length', null); 
     37    $this->setOption('min_length', null); 
    3638    $this->setOption('empty_value', ''); 
    3739  } 
     
    5860    return $clean; 
    5961  } 
    60  
    61   /** 
    62    * @see sfValidator 
    63    */ 
    64   public function getErrorCodes() 
    65   { 
    66     return array_merge(parent::getErrorCodes(), array('max_length', 'min_length')); 
    67   } 
    6862} 
  • trunk/lib/validator/sfValidatorUrl.class.php

    r5753 r5816  
    2222   * @see sfValidatorRegex 
    2323   */ 
    24   public function __construct($options = array(), $messages = array()) 
     24  public function configure($options = array(), $messages = array()) 
    2525  { 
    26     $options['pattern'] = '~^ 
     26    $this->setOption('pattern', '~^ 
    2727      https?://                               # http or https 
    2828      ( 
     
    3333      (:[0-9]+)?                              # a port (optional) 
    3434      (/?|/\S+)                               # a /, nothing or a / with something 
    35     $~ix'; 
    36  
    37     parent::__construct($options, $messages); 
     35    $~ix'); 
    3836  } 
    3937} 
  • trunk/test/unit/validator/sfValidatorBooleanTest.php

    r5634 r5816  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(17, new lime_output_color()); 
     13$t = new lime_test(16, new lime_output_color()); 
    1414 
    1515$v = new sfValidatorBoolean(); 
    16  
    17 // ->getErrorCodes() 
    18 $t->diag('->getErrorCodes()'); 
    19 $t->is($v->getErrorCodes(), array('required', 'invalid'), '->getErrorCodes() returns all possible error codes'); 
    2016 
    2117// ->clean() 
  • trunk/test/unit/validator/sfValidatorDateTest.php

    r5581 r5816  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(16, new lime_output_color()); 
     13$t = new lime_test(18, new lime_output_color()); 
    1414 
    1515$v = new sfValidatorDate(); 
    16  
    17 // ->getErrorCodes() 
    18 $t->diag('->getErrorCodes()'); 
    19 $t->is($v->getErrorCodes(), array('required', 'invalid', 'date_format'), '->getErrorCodes() returns all possible error codes'); 
    2016 
    2117// ->clean() 
     
    4339$t->diag('validate timestamp'); 
    4440$t->is($v->clean(time()), date('Y-m-d', time()), '->clean() accepts timestamps as input'); 
     41 
     42// validate date array 
     43$t->diag('validate date array'); 
     44$t->is($v->clean(array('year' => 2005, 'month' => 10, 'day' => 15)), '2005-10-15', '->clean() accepts an array as an input'); 
     45 
     46try 
     47{ 
     48  $v->clean(array('year' => -2, 'month' => 1, 'day' => 15)); 
     49  $t->fail('->clean() throws a sfValidatorError if the date is not valid'); 
     50} 
     51catch (sfValidatorError $e) 
     52{ 
     53  $t->pass('->clean() throws a sfValidatorError if the date is not valid'); 
     54} 
    4555 
    4656// validate regex 
     
    7787$t->diag('option with_time'); 
    7888$v->setOption('with_time', true); 
     89$t->is($v->clean(array('year' => 2005, 'month' => 10, 'day' => 15, 'hour' => 12, 'minute' => 10, 'second' => 15)), '2005-10-15 12:10:15', '->clean() accepts an array as an input'); 
    7990$t->is($v->clean('18 october 2005 12:30'), '2005-10-18 12:30:00', '->clean() can accept date time with the with_time option'); 
    8091$t->is($v->clean(time()), date('Y-m-d H:i:s', time()), '->clean() can accept date time with the with_time option'); 
  • trunk/test/unit/validator/sfValidatorDecoratorTest.php

    r5641 r5816  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(15, new lime_output_color()); 
     13$t = new lime_test(18, new lime_output_color()); 
    1414 
    1515class MyValidator extends sfValidatorDecorator 
    1616{ 
    17   protected function getValidator() 
     17  public function getValidator() 
    1818  { 
    1919    return new sfValidatorString(array('min_length' => 2, 'trim' => true), array('required' => 'This string is required.')); 
     
    3838} 
    3939 
     40$v = new MyValidator(); 
     41 
    4042// ->getErrorCodes() 
    4143$t->diag('->getErrorCodes()'); 
    42 $t->is($v->getErrorCodes(), array('required', 'invalid', 'max_length', 'min_length'), '->getErrorCodes() returns error codes form the embedded validator'); 
     44$t->is($v->getErrorCodes(), $v->getValidator()->getErrorCodes(), '->getErrorCodes() is a proxy to the embedded validator method'); 
     45 
     46// ->asString() 
     47$t->diag('->asString()'); 
     48$t->is($v->asString(), $v->getValidator()->asString(), '->asString() is a proxy to the embedded validator method'); 
     49 
     50// ->getDefaultMessages() 
     51$t->diag('->getDefaultMessages()'); 
     52$t->is($v->getDefaultMessages(), $v->getValidator()->getDefaultMessages(), '->getDefaultMessages() is a proxy to the embedded validator method'); 
     53 
     54// ->getDefaultOptions() 
     55$t->diag('->getDefaultOptions()'); 
     56$t->is($v->getDefaultOptions(), $v->getValidator()->getDefaultOptions(), '->getDefaultOptions() is a proxy to the embedded validator method'); 
    4357 
    4458// ->getMessage() ->getMessages() ->setMessage() ->setMessages() 
     
    5569$t->is($v->getOption('trim'), true, '->getOption() returns an option from the embedded validator'); 
    5670$v->setOption('trim', false); 
    57 $t->is($v->getOptions(), array('required' => true, 'trim' => false, 'empty_value' => '', 'min_length' => 2), '->getOptions() returns an array of options from the embedded validator'); 
     71$t->is($v->getOptions(), array('required' => true, 'trim' => false, 'empty_value' => '', 'min_length' => 2, 'max_length' => null), '->getOptions() returns an array of options from the embedded validator'); 
    5872$t->is($v->hasOption('min_length'), true, '->hasOption() returns true if the embedded validator has a given option'); 
    5973$v->setOptions(array('min_length' => 10)); 
  • trunk/test/unit/validator/sfValidatorFromDescriptionTest.php

    r5757 r5816  
    104104 
    105105  'email:Email and (age:Integer({min: 18}) or (age:Integer({max: 18}) and is_young:Boolean({required: true})))', 
    106   '(password == password_bis) and begin_date <= end_date and password:String({min: 4, max: 18})', 
    107   'countries:Choice({choices: [France, USA, Italy, Spain]}) and password ==({invalid: "Passwords must be the same (%left_field% != %right_field%)"}) password_bis and begin_date <= end_date and password:String({min: 4, max: 18})', 
     106  '(password == password_bis) and begin_date <= end_date and password:String({min_length: 4, max_length: 18})',&nb