protected function doClean($values)
 {
     if (!call_user_func($this->getOption('callable'), $values[$this->getOption('conditional_field')])) {
         return $values;
     }
     // get the validators and turn them into an sfValidatorSchema for easy processing
     $validators = $this->getOption('conditional_validators');
     if (is_array($validators)) {
         $validators = new sfValidatorSchema($validators);
     } elseif (!$validators instanceof sfValidatorSchema) {
         throw new sfException('Options conditional_validators must be an array or an instance of sfValidatorSchema.');
     }
     // have the validator schema simply ignore the extra fields
     $validators->setOption('allow_extra_fields', true);
     $validators->setOption('filter_extra_fields', false);
     return $validators->clean($values);
 }
Exemple #2
0
    $t->is($e[0]->getCode(), 's1_not_equal_s2', '->clean() throws an exception with all error messages');
}
$v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2));
$t->is($v->clean(array('s1' => 'foo')), array('s1' => 'foo', 's2' => null), '->clean() returns null values for fields not present in the input array');
$t->diag('extra fields');
$v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2));
$v->setOption('allow_extra_fields', true);
$ret = $v->clean(array('s1' => 'foo', 's2' => 'bar', 'foo' => 'bar'));
$t->is($ret, array('s1' => 'foo', 's2' => 'bar'), '->clean() filters non existant fields if "allow_extra_fields" is true');
$v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2), array('allow_extra_fields' => true));
$ret = $v->clean(array('s1' => 'foo', 's2' => 'bar', 'foo' => 'bar'));
$t->is($ret, array('s1' => 'foo', 's2' => 'bar'), '->clean() filters non existant fields if "allow_extra_fields" is true');
$v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2), array('allow_extra_fields' => true, 'filter_extra_fields' => false));
$ret = $v->clean(array('s1' => 'foo', 's2' => 'bar', 'foo' => 'bar'));
$t->is($ret, array('s1' => 'foo', 's2' => 'bar', 'foo' => 'bar'), '->clean() do not filter non existant fields if "filter_extra_fields" is false');
$v->setOption('filter_extra_fields', false);
$ret = $v->clean(array('s1' => 'foo', 's2' => 'bar', 'foo' => 'bar'));
$t->is($ret, array('s1' => 'foo', 's2' => 'bar', 'foo' => 'bar'), '->clean() do not filter non existant fields if "filter_extra_fields" is false');
$t->diag('one validator fails');
$v['s2']->setOption('max_length', 2);
try {
    $v->clean(array('s1' => 'foo', 's2' => 'bar'));
    $t->fail('->clean() throws an sfValidatorErrorSchema exception if one of the validators fails');
    $t->skip('', 2);
} catch (sfValidatorErrorSchema $e) {
    $t->pass('->clean() throws an sfValidatorErrorSchema exception if one of the validators fails');
    $t->is(count($e), 1, '->clean() throws an exception with all error messages');
    $t->is($e['s2']->getCode(), 'max_length', '->clean() throws an exception with all error messages');
}
$t->diag('several validators fail');
$v['s1']->setOption('max_length', 2);