Development

Changeset 5882

You must first sign up to be able to contribute.

Changeset 5882

Show
Ignore:
Timestamp:
11/06/07 20:58:15 (2 years ago)
Author:
fabien
Message:

validators: fixed API docs, added addOption() and addMessage() methods to register options and messages

Files:

Legend:

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

    r5816 r5882  
    4040   *  * empty_value: empty value when value is not required 
    4141   * 
     42   * Available error codes: 
     43   * 
     44   *  * required 
     45   *  * invalid 
     46   * 
    4247   * @param array An array of options 
    4348   * @param array An array of error messages 
     
    6671 
    6772    // check required options 
    68     if ($diff = array_diff($this->requiredOptions, array_keys($options))) 
     73    if ($diff = array_diff($this->requiredOptions, array_merge(array_keys($this->options), array_keys($options)))) 
    6974    { 
    7075      throw new sfException(sprintf('%s requires the following options: \'%s\'.', get_class($this), implode('\', \'', $diff))); 
     
    107112 
    108113  /** 
    109    * Changes an error message given the error code. 
     114   * Adds a new error code with a default error message. 
    110115   * 
    111116   * @param string The error code 
    112117   * @param string The error message 
    113118   */ 
     119  public function addMessage($name, $value) 
     120  { 
     121    $this->messages[$name] = $value; 
     122  } 
     123 
     124  /** 
     125   * Changes an error message given the error code. 
     126   * 
     127   * @param string The error code 
     128   * @param string The error message 
     129   */ 
    114130  public function setMessage($name, $value) 
    115131  { 
     132    if (!in_array($name, array_keys($this->messages))) 
     133    { 
     134      throw new sfException(sprintf('%s does not support the following error code: \'%s\'.', get_class($this), $name)); 
     135    } 
     136 
    116137    $this->messages[$name] = $value; 
    117138  } 
     
    147168  { 
    148169    return isset($this->options[$name]) ? $this->options[$name] : null; 
     170  } 
     171 
     172  /** 
     173   * Adds a new option value with a default value. 
     174   * 
     175   * @param string The option name 
     176   * @param mixed  The default value 
     177   */ 
     178  public function addOption($name, $value = null) 
     179  { 
     180    $this->options[$name] = $value; 
    149181  } 
    150182 
     
    157189  public function setOption($name, $value) 
    158190  { 
     191    if (!in_array($name, array_merge(array_keys($this->options), $this->requiredOptions))) 
     192    { 
     193      throw new sfException(sprintf('%s does not support the following option: \'%s\'.', get_class($this), $name)); 
     194    } 
     195 
    159196    $this->options[$name] = $value; 
    160197  } 
     
    190227  { 
    191228    $this->options = $values; 
     229  } 
     230 
     231  /** 
     232   * Adds a required option. 
     233   * 
     234   * @param string The option name 
     235   */ 
     236  public function addRequiredOption($name) 
     237  { 
     238    $this->requiredOptions[] = $name; 
     239  } 
     240 
     241  /** 
     242   * Returns all required option names. 
     243   * 
     244   * @param array An array of required option names 
     245   */ 
     246  public function getRequiredOptions() 
     247  { 
     248    return $this->requiredOptions; 
    192249  } 
    193250 
     
    333390  { 
    334391    $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; 
    355392  } 
    356393 
  • trunk/lib/validator/sfValidatorBoolean.class.php

    r5816 r5882  
    3131  protected function configure($options = array(), $messages = array()) 
    3232  { 
    33     $this->setOption('true_values', array('true', 't', 'yes', 'y', 'on', '1')); 
    34     $this->setOption('false_values', array('false', 'f', 'no', 'n', 'off', '0')); 
     33    $this->addOption('true_values', array('true', 't', 'yes', 'y', 'on', '1')); 
     34    $this->addOption('false_values', array('false', 'f', 'no', 'n', 'off', '0')); 
     35 
    3536    $this->setOption('required', false); 
    3637  } 
  • trunk/lib/validator/sfValidatorCallback.class.php

    r5816 r5882  
    3131  { 
    3232    $this->addRequiredOption('callback'); 
     33 
    3334    $this->setOption('required', false); 
    3435  } 
  • trunk/lib/validator/sfValidatorDate.class.php

    r5816 r5882  
    2424   * Available options: 
    2525   * 
    26    *  * date_format:     A regular expression that dates must match 
    27    *  * with_time:       true if the validator must return a time, false otherwise 
    28    *  * date_output:     The format to use when returning a date (default to Y-m-d) 
    29    *  * datetime_output: The format to use when returning a date with time (default to Y-m-d H:i:s) 
     26   *  * date_format:       A regular expression that dates must match 
     27   *  * with_time:         true if the validator must return a time, false otherwise 
     28   *  * date_output:       The format to use when returning a date (default to Y-m-d) 
     29   *  * datetime_output:   The format to use when returning a date with time (default to Y-m-d H:i:s) 
     30   *  * date_format_error: The date format to use when displaying an error for a bad_format error 
     31   * 
     32   * Available error codes: 
     33   * 
     34   *  * bad_format 
    3035   * 
    3136   * @see sfValidator 
     
    3338  protected function configure($options = array(), $messages = array()) 
    3439  { 
    35     $this->setMessage('bad_format', '"%value%" does not match the date format (%date_format%).'); 
     40    $this->addMessage('bad_format', '"%value%" does not match the date format (%date_format%).'); 
    3641 
    37     $this->setOption('date_format', null); 
    38     $this->setOption('with_time', false); 
    39     $this->setOption('date_output', 'Y-m-d'); 
    40     $this->setOption('datetime_output', 'Y-m-d H:i:s'); 
     42    $this->addOption('date_format', null); 
     43    $this->addOption('with_time', false); 
     44    $this->addOption('date_output', 'Y-m-d'); 
     45    $this->addOption('datetime_output', 'Y-m-d H:i:s'); 
     46    $this->addOption('date_format_error'); 
    4147  } 
    4248 
  • trunk/lib/validator/sfValidatorEmail.class.php

    r5816 r5882  
    2424  protected function configure($options = array(), $messages = array()) 
    2525  { 
     26    parent::configure($options, $messages); 
     27 
    2628    $this->setOption('pattern', '/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i'); 
    2729  } 
  • trunk/lib/validator/sfValidatorInteger.class.php

    r5816 r5882  
    2727   *  * min: The minimum value allowed 
    2828   * 
     29   * Available error codes: 
     30   * 
     31   *  * max 
     32   *  * min 
     33   * 
    2934   * @see sfValidator 
    3035   */ 
    3136  protected function configure($options = array(), $messages = array()) 
    3237  { 
    33     $this->setMessage('max', '"%value%" must be less than %max%.'); 
    34     $this->setMessage('min', '"%value%" must be greater than %min%.'); 
     38    $this->addMessage('max', '"%value%" must be less than %max%.'); 
     39    $this->addMessage('min', '"%value%" must be greater than %min%.'); 
     40 
     41    $this->addOption('min'); 
     42    $this->addOption('max'); 
     43 
    3544    $this->setMessage('invalid', '"%value%" is not an integer.'); 
    36  
    37     $this->setOption('min', null); 
    38     $this->setOption('max', null); 
    3945  } 
    4046 
  • trunk/lib/validator/sfValidatorNumber.class.php

    r5816 r5882  
    2727   *  * min: The minimum value allowed 
    2828   * 
     29   * Available error codes: 
     30   * 
     31   *  * max 
     32   *  * min 
     33   * 
    2934   * @see sfValidator 
    3035   */ 
    3136  protected function configure($options = array(), $messages = array()) 
    3237  { 
    33     $this->setMessage('max', '"%value%" is too long (largest allowed is %max%).'); 
    34     $this->setMessage('min', '"%value%" is too short (smallest allowed is %min%).'); 
     38    $this->addMessage('max', '"%value%" is too long (largest allowed is %max%).'); 
     39    $this->addMessage('min', '"%value%" is too short (smallest allowed is %min%).'); 
     40 
     41    $this->addOption('min'); 
     42    $this->addOption('max'); 
     43 
    3544    $this->setMessage('invalid', '"%value%" is not a number.'); 
    36  
    37     $this->setOption('min', null); 
    38     $this->setOption('max', null); 
    3945  } 
    4046 
  • trunk/lib/validator/sfValidatorSchema.class.php

    r5816 r5882  
    6363   *  * filter_extra_fields: if true, the validator filters extra fields from the returned array of cleaned values (default to true) 
    6464   * 
     65   * Available error codes: 
     66   * 
     67   *  * extra_fields 
     68   * 
    6569   * @param array An array of options 
    6670   * @param array An array of error messages 
     
    7074  public function configure($options = array(), $messages = array()) 
    7175  { 
    72     $this->setOption('allow_extra_fields', false); 
    73     $this->setOption('filter_extra_fields', true); 
    74  
    75     $this->setMessage('extra_fields', 'Extra field %field%.'); 
     76    $this->addOption('allow_extra_fields', false); 
     77    $this->addOption('filter_extra_fields', true); 
     78 
     79    $this->addMessage('extra_fields', 'Extra field %field%.'); 
    7680  } 
    7781 
  • trunk/lib/validator/sfValidatorSchemaCompare.class.php

    r5816 r5882  
    4848  public function __construct($leftField, $operator, $rightField, $options = array(), $messages = array()) 
    4949  { 
    50     $this->setOption('left_field', $leftField); 
    51     $this->setOption('operator', $operator); 
    52     $this->setOption('right_field', $rightField); 
     50    $this->addOption('left_field', $leftField); 
     51    $this->addOption('operator', $operator); 
     52    $this->addOption('right_field', $rightField); 
    5353 
    5454    parent::__construct(null, $options, $messages); 
  • trunk/lib/validator/sfValidatorSchemaFilter.class.php

    r5816 r5882  
    3131  public function __construct($field, sfValidator $validator, $options = array(), $messages = array()) 
    3232  { 
    33     $this->setOption('field', $field); 
    34     $this->setOption('validator', $validator); 
     33    $this->addOption('field', $field); 
     34    $this->addOption('validator', $validator); 
    3535 
    3636    parent::__construct(null, $options, $messages); 
  • trunk/lib/validator/sfValidatorString.class.php

    r5816 r5882  
    2727   *  * min_length: The minimum length of the string 
    2828   * 
     29   * Available error codes: 
     30   * 
     31   *  * max_length 
     32   *  * min_length 
     33   * 
    2934   * @see sfValidator 
    3035   */ 
    3136  protected function configure($options = array(), $messages = array()) 
    3237  { 
    33     $this->setMessage('max_length', '"%value%" is too long (%max_length% characters max).'); 
    34     $this->setMessage('min_length', '"%value%" is too short (%min_length% characters min).'); 
     38    $this->addMessage('max_length', '"%value%" is too long (%max_length% characters max).'); 
     39    $this->addMessage('min_length', '"%value%" is too short (%min_length% characters min).'); 
    3540 
    36     $this->setOption('max_length', null); 
    37     $this->setOption('min_length', null); 
     41    $this->addOption('max_length'); 
     42    $this->addOption('min_length'); 
     43 
    3844    $this->setOption('empty_value', ''); 
    3945  } 
  • trunk/lib/validator/sfValidatorUrl.class.php

    r5816 r5882  
    2424  public function configure($options = array(), $messages = array()) 
    2525  { 
     26    parent::configure($options, $messages); 
     27 
    2628    $this->setOption('pattern', '~^ 
    2729      https?://                               # http or https 
  • trunk/test/unit/validator/sfValidatorChoiceManyTest.php

    r5757 r5882  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(5, new lime_output_color()); 
     13$t = new lime_test(6, new lime_output_color()); 
    1414 
    1515$v = new sfValidatorChoiceMany(array('choices' => array('foo', 'bar'))); 
     
    4040  $t->pass('->clean() throws an sfValidatorError if the value is not an expected value'); 
    4141} 
     42 
     43function choice_callable() 
     44{ 
     45  return array(1, 2, 3); 
     46} 
     47 
     48// choices as a callable 
     49$t->diag('choices as a callable'); 
     50$v = new sfValidatorChoiceMany(array('choices' => new sfCallable('choice_callable'))); 
     51$t->is($v->clean(array('2')), array('2'), '__construct() can take a sfCallable object as a choices option'); 
  • trunk/test/unit/validator/sfValidatorTest.php

    r5816 r5882  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(33, new lime_output_color()); 
     13$t = new lime_test(37, new lime_output_color()); 
    1414 
    1515class ValidatorIdentity extends sfValidator 
     
    1717  protected function configure($options = array(), $messages = array()) 
    1818  { 
    19     $this->setOption('foo', 'bar'); 
    20     $this->setMessage('foo', 'bar'); 
     19    $this->addOption('foo', 'bar'); 
     20    $this->addMessage('foo', 'bar'); 
    2121  } 
    2222 
     
    7575} 
    7676 
     77// ->getRequiredOptions() 
    7778$t->diag('getRequiredOptions'); 
    7879$v = new ValidatorIdentityWithRequired(array('foo' => 'bar')); 
     
    118119$v->setOption('trim', true); 
    119120$t->is($v->clean('  foo  '), 'foo', '->setOption() can turn on whitespace trimming'); 
     121try 
     122{ 
     123  $v->setOption('foobar', 'foo'); 
     124  $t->fail('->setOption() throws an sfException if the option is not registered'); 
     125} 
     126catch (sfException $e) 
     127{ 
     128  $t->pass('->setOption() throws an sfException if the option is not registered'); 
     129} 
    120130 
    121131// ->hasOption() 
     
    129139$t->is($v->getOption('nonexistant'), null, '->getOption() returns null if the option does not exist'); 
    130140 
     141// ->addOption() 
     142$t->diag('->addOption()'); 
     143$v->addOption('foobar'); 
     144$v->setOption('foobar', 'foo'); 
     145$t->is($v->getOption('foobar'), 'foo', '->addOption() adds a new option to a validator'); 
     146 
    131147// ->getOptions() ->setOptions() 
    132148$t->diag('->getOptions() ->setOptions()'); 
     
    156172} 
    157173 
     174try 
     175{ 
     176  $v->setMessage('foobar', 'foo'); 
     177  $t->fail('->setMessage() throws an sfException if the message is not registered'); 
     178} 
     179catch (sfException $e) 
     180{ 
     181  $t->pass('->setMessage() throws an sfException if the message is not registered'); 
     182} 
     183 
    158184// ->setMessages() 
    159185$t->diag('->setMessages()'); 
    160186$v->setMessages(array('required' => 'This is required.')); 
    161187$t->is($v->getMessages(), array('required' => 'This is required.'), '->setMessages() changes all error messages'); 
     188 
     189// ->addMessage() 
     190$t->diag('->addMessage()'); 
     191$v->addMessage('foobar', 'foo'); 
     192$v->setMessage('foobar', 'bar'); 
     193$t->is($v->getMessage('foobar'), 'bar', '->addMessage() adds a new error code'); 
    162194 
    163195// ->getErrorCodes() 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.