예제 #1
0
 /** Converts an sfValidatorErrorSchema into an array of error messages.
  *
  * @param sfValidatorErrorSchema $schema
  *
  * @return array
  */
 private function _convertErrorSchema(sfValidatorErrorSchema $schema)
 {
     $errors = array();
     foreach ($schema->getErrors() as $key => $error) {
         $errors[$key] = $error instanceof sfValidatorErrorSchema ? $this->_convertErrorSchema($error) : (string) $error;
     }
     return $errors;
 }
$v2 = new sfValidatorString();
$e1 = new sfValidatorError($v1, 'max_length', array('value' => 'foo', 'max_length' => 1));
$e2 = new sfValidatorError($v2, 'min_length', array('value' => 'bar', 'min_length' => 5));
$e = new sfValidatorErrorSchema($v1);
// __construct()
$t->diag('__construct()');
$t->is($e->getValidator(), $v1, '__construct() takes a sfValidator as its first argument');
$e = new sfValidatorErrorSchema($v1, array('e1' => $e1, 'e2' => $e2));
$t->is($e->getErrors(), array('e1' => $e1, 'e2' => $e2), '__construct() can take an array of sfValidatorError as its second argument (depreciated)');
// ->addError() ->getErrors()
$t->diag('->addError() ->getErrors()');
$e = new sfValidatorErrorSchema($v1);
$e->addError($e1);
$e->addError($e2, 'e2');
$e->addError($e1, '2');
$t->is($e->getErrors(), array($e1, 'e2' => $e2, '2' => $e1), '->addError() adds an error to the error schema');
$t->diag('embedded errors');
$es1 = new sfValidatorErrorSchema(new sfValidatorString());
$es1->addError($e1);
$es1->addError($e1, 'e1');
$es1->addError($e2, 'e2');
$es = new sfValidatorErrorSchema(new sfValidatorString());
$es->addError($e1);
$es->addError($e1, 'e1');
$es->addError($es1, 'e2');
$es->addError($e2, 'e1');
$t->is($es->getCode(), 'max_length e1 [max_length min_length] e2 [max_length e1 [max_length] e2 [min_length]]', '->addError() adds an error to the error schema');
$es->addError($e2);
$t->is($es->getCode(), 'max_length min_length e1 [max_length min_length] e2 [max_length e1 [max_length] e2 [min_length]]', '->addError() adds an error to the error schema');
$es2 = new sfValidatorErrorSchema(new sfValidatorString());
$es2->addError($e1);
예제 #3
0
 /**
  * @param sfForm $form
  * @param sfValidatorErrorSchema $errorSchema
  * @return array
  */
 protected static function errorSchema2ArrayLabel($form, $errorSchema, $errors = array(), $default_label = null)
 {
     foreach ($errorSchema->getErrors() as $key => $error) {
         /* @var $error sfValidatorError */
         $label = $default_label ? $default_label : self::getLabel($form, $key);
         if ($error instanceof sfValidatorErrorSchema) {
             $errors = self::errorSchema2ArrayLabel($form, $error, $errors, $label);
         } else {
             $errors[$label][] = $form->getWidgetSchema()->getFormFormatter()->translate($error->getMessageFormat(), $error->getArguments());
         }
     }
     return $errors;
 }