/** * @see sfValidatorBase */ protected function doClean($value) { $clean = array(); $clean['value'] = $this->getOption('validator')->clean($value['value']); if ($this->getOption('is_edit_public_flag')) { if (!isset($value['public_flag'])) { throw new sfValidatorError($this, 'invalid'); } $validator = new sfValidatorChoice(array('choices' => array_keys($this->profile->getPublicFlags()))); $clean['public_flag'] = $validator->clean($value['public_flag']); } return $clean; }
/** * @see sfValidatorBase */ protected function doClean($value) { $clean = array(); $clean['value'] = $this->getOption('validator')->clean($value['value']); if ($this->getOption('is_edit_public_flag')) { if (!isset($value['public_flag'])) { throw new sfValidatorError($this, 'invalid'); } $validator = new sfValidatorChoice(array('choices' => array_keys($this->profile->getPublicFlags()))); $clean['public_flag'] = $validator->clean($value['public_flag']); } if ($this->profile->getIsUnique() && in_array($this->profile->getFormType(), array('input', 'textarea'))) { $profileId = $this->profile->getId(); $memberId = sfContext::getInstance()->getUser()->getMemberId(); $isDuplicate = Doctrine::getTable('MemberProfile')->isDuplicatedProfileValue($profileId, $value['value'], $memberId); if ($isDuplicate) { throw new sfValidatorError($this, 'The inputted value is already exist.'); } } return $clean; }
$t->diag('choices as a callable'); $v = new sfValidatorChoice(array('choices' => new sfCallable('choice_callable'))); $t->is($v->clean('2'), '2', '__construct() can take a sfCallable object as a choices option'); // see bug #4212 $v = new sfValidatorChoice(array('choices' => array(0, 1, 2))); try { $v->clean('xxx'); $t->fail('->clean() throws an sfValidatorError if the value is not strictly an expected value'); $t->skip('', 1); } catch (sfValidatorError $e) { $t->pass('->clean() throws an sfValidatorError if the value is not strictly an expected value'); $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError'); } // min/max options $v = new sfValidatorChoice(array('multiple' => true, 'choices' => array(0, 1, 2, 3, 4, 5), 'min' => 2, 'max' => 3)); try { $v->clean(array(0)); $t->fail('->clean() throws an sfValidatorError if the minimum number of values are not selected'); $t->skip('', 1); } catch (sfValidatorError $e) { $t->pass('->clean() throws an sfValidatorError if the minimum number of values are not selected'); $t->is($e->getCode(), 'min', '->clean() throws a sfValidatorError'); } try { $v->clean(array(0, 1, 2, 3)); $t->fail('->clean() throws an sfValidatorError if more than the maximum number of values are selected'); $t->skip('', 1); } catch (sfValidatorError $e) { $t->pass('->clean() throws an sfValidatorError if more than the maximum number of values are selected'); $t->is($e->getCode(), 'max', '->clean() throws a sfValidatorError'); }
$t->skip('', 1); } catch (sfValidatorError $e) { $t->pass('->clean() throws an sfValidatorError if the value is not an expected value'); $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError'); } try { $v->clean(array('foobar', 'bar')); $t->fail('->clean() throws an sfValidatorError if the value is not an expected value'); $t->skip('', 1); } catch (sfValidatorError $e) { $t->pass('->clean() throws an sfValidatorError if the value is not an expected value'); $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError'); } function choice_callable() { return array(1, 2, 3); } // choices as a callable $t->diag('choices as a callable'); $v = new sfValidatorChoiceMany(array('choices' => new sfCallable('choice_callable'))); $t->is($v->clean(array('2')), array('2'), '__construct() can take a sfCallable object as a choices option'); // see bug #4212 $v = new sfValidatorChoice(array('choices' => array(0, 1, 2, 3, 4, 5))); try { $v->clean(array('xxx', 'yyy')); $t->fail('->clean() throws an sfValidatorError if the values are not strictly the expected values'); $t->skip('', 1); } catch (sfValidatorError $e) { $t->pass('->clean() throws an sfValidatorError if the values are not strictly the expected values'); $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError'); }
} $v = new sfValidatorChoice(array('choices' => array('foo', 'bar'))); // ->clean() $t->diag('->clean()'); $t->is($v->clean('foo'), 'foo', '->clean() checks that the value is an expected value'); $t->is($v->clean('bar'), 'bar', '->clean() checks that the value is an expected value'); try { $v->clean('foobar'); $t->fail('->clean() throws an sfValidatorError if the value is not an expected value'); $t->skip('', 1); } catch (sfValidatorError $e) { $t->pass('->clean() throws an sfValidatorError if the value is not an expected value'); $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError'); } // ->asString() $t->diag('->asString()'); $t->is($v->asString(), 'Choice({ choices: [foo, bar] })', '->asString() returns a string representation of the validator'); // choices as a callable $t->diag('choices as a callable'); $v = new sfValidatorChoice(array('choices' => new sfCallable('choice_callable'))); $t->is($v->clean('2'), '2', '__construct() can take a sfCallable object as a choices option'); // see bug #4212 $v = new sfValidatorChoice(array('choices' => array(0, 1, 2))); try { $v->clean('xxx'); $t->fail('->clean() throws an sfValidatorError if the value is not strictly an expected value'); $t->skip('', 1); } catch (sfValidatorError $e) { $t->pass('->clean() throws an sfValidatorError if the value is not strictly an expected value'); $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError'); }