Development

Changeset 5819

You must first sign up to be able to contribute.

Changeset 5819

Show
Ignore:
Timestamp:
11/02/07 18:30:38 (10 months ago)
Author:
fabien
Message:

added callable support to the choices option for the sfValidatorChoices class

Files:

Legend:

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

    r5816 r5819  
    3838  protected function doClean($value) 
    3939  { 
    40     if (!in_array($value, $this->getOption('choices'))) 
     40    $choices = $this->getOption('choices'); 
     41    if ($choices instanceof sfCallable) 
     42    { 
     43      $choices = $choices->call(); 
     44    } 
     45 
     46    if (!in_array($value, $choices)) 
    4147    { 
    4248      throw new sfValidatorError($this, 'invalid', array('value' => $value)); 
  • trunk/test/unit/validator/sfValidatorChoiceTest.php

    r5757 r5819  
    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()); 
     14 
     15function choice_callable() 
     16
     17  return array(1, 2, 3); 
     18
    1419 
    1520// __construct() 
     
    4550$t->diag('->asString()'); 
    4651$t->is($v->asString(), 'Choice({ choices: [foo, bar] })', '->asString() returns a string representation of the validator'); 
     52 
     53// choices as a callable 
     54$t->diag('choices as a callable'); 
     55$v = new sfValidatorChoice(array('choices' => new sfCallable('choice_callable'))); 
     56$t->is($v->clean('2'), '2', '__construct() can take a sfCallable object as a choices option');