Пример #1
0
 /**
  * Constructor.
  *
  * The first argument can be:
  *
  *  * null
  *  * a Base instance
  *  * an array of Base instances
  *
  * @param mixed $validators Initial validators
  * @param array $options    An array of options
  * @param array $messages   An array of error messages
  *
  * @see Base
  */
 public function __construct($validators = null, $options = array(), $messages = array())
 {
     if ($validators instanceof Base) {
         $this->addValidator($validators);
     } else {
         if (is_array($validators)) {
             foreach ($validators as $validator) {
                 $this->addValidator($validator);
             }
         } else {
             if (null !== $validators) {
                 throw new \InvalidArgumentException('And constructor takes a Base object, or a Base array.');
             }
         }
     }
     parent::__construct($options, $messages);
 }
Пример #2
0
$t->diag('->setMessages()');
$v->setMessages(array('required' => 'This is required.'));
$t->is($v->getMessages(), array('required' => 'This is required.'), '->setMessages() changes all error messages');
// ->addMessage()
$t->diag('->addMessage()');
$v->addMessage('foobar', 'foo');
$v->setMessage('foobar', 'bar');
$t->is($v->getMessage('foobar'), 'bar', '->addMessage() adds a new error code');
// ->getErrorCodes()
$t->diag('->getErrorCodes()');
$t->is($v->getErrorCodes(), array('required', 'invalid', 'foo'), '->getErrorCodes() returns an array of error codes the validator can use');
// ::getCharset() ::setCharset()
$t->diag('::getCharset() ::setCharset()');
$t->is(Base::getCharset(), 'UTF-8', '::getCharset() returns the charset to use for validators');
Base::setCharset('ISO-8859-1');
$t->is(Base::getCharset(), 'ISO-8859-1', '::setCharset() changes the charset to use for validators');
// ->asString()
$t->diag('->asString()');
$v = new ValidatorIdentity();
$t->is($v->asString(), 'ValidatorIdentity()', '->asString() returns a string representation of the validator');
$v->setOption('required', false);
$v->setOption('foo', 'foo');
$t->is($v->asString(), 'ValidatorIdentity({ required: false, foo: foo })', '->asString() returns a string representation of the validator');
$v->setMessage('required', 'This is required.');
$t->is($v->asString(), 'ValidatorIdentity({ required: false, foo: foo }, { required: \'This is required.\' })', '->asString() returns a string representation of the validator');
$v = new ValidatorIdentity();
$v->setMessage('required', 'This is required.');
$t->is($v->asString(), 'ValidatorIdentity({}, { required: \'This is required.\' })', '->asString() returns a string representation of the validator');
// ::setDefaultMessage()
$t->diag('::setDefaultMessage()');
ValidatorIdentity::setDefaultMessage('required', 'This field is required.');