/** * Returns the form field associated with the name (implements the ArrayAccess interface). * * @param string $name The offset of the value to get * * @return sfFormField A form field instance */ public function offsetGet($name) { if (!isset($this->fields[$name])) { if (is_null($widget = $this->widget[$name])) { throw new InvalidArgumentException(sprintf('Widget "%s" does not exist.', $name)); } $error = isset($this->error[$name]) ? $this->error[$name] : null; if ($widget instanceof sfWidgetFormSchema) { $class = 'sfFormFieldSchema'; if ($error && !$error instanceof sfValidatorErrorSchema) { $error = new sfValidatorErrorSchema($error->getValidator(), array($error)); } } else { $class = 'sfFormField'; } $this->fields[$name] = new $class($widget, $this, $name, isset($this->value[$name]) ? $this->value[$name] : null, $error); } return $this->fields[$name]; }
/** * Renders a field by name. * * @param string $name The field name * @param string $value The field value * @param array $attributes An array of HTML attributes to be merged with the current HTML attributes * @param array $errors An array of errors for the field * * @return string An HTML string representing the rendered widget */ public function renderField($name, $value = null, $attributes = array(), $errors = array()) { if (is_null($widget = $this[$name])) { throw new InvalidArgumentException(sprintf('The field named "%s" does not exist.', $name)); } if ($widget instanceof sfWidgetFormSchema && $errors && !$errors instanceof sfValidatorErrorSchema) { $errors = new sfValidatorErrorSchema($errors->getValidator(), array($errors)); } // we clone the widget because we want to change the id format temporarily $clone = clone $widget; $clone->setIdFormat($this->options['id_format']); return $clone->render($this->generateName($name), $value, array_merge($clone->getAttributes(), $attributes), $errors); }
$t->diag('implements Serializable'); class NotSerializable implements Serializable { public function serialize() { throw new Exception('Not serializable'); } public function unserialize($serialized) { throw new Exception('Not serializable'); } } function will_crash($a) { return serialize(new sfValidatorErrorSchema(new sfValidatorString())); } $a = new NotSerializable(); try { $serialized = will_crash($a); $t->pass('"sfValidatorErrorSchema" implements Serializable'); } catch (Exception $e) { $t->fail('"sfValidatorErrorSchema" implements Serializable'); } $e = new sfValidatorErrorSchema($v1); $e1 = unserialize($serialized); $t->is($e1->getMessage(), $e->getMessage(), '"sfValidatorErrorSchema" implements Serializable'); $t->is($e1->getCode(), $e->getCode(), '"sfValidatorErrorSchema" implements Serializable'); $t->is(get_class($e1->getValidator()), get_class($e->getValidator()), '"sfValidatorErrorSchema" implements Serializable'); $t->is($e1->getArguments(), $e->getArguments(), '"sfValidatorErrorSchema" implements Serializable'); $t->is($e1->getNamedErrors(), $e->getNamedErrors(), '"sfValidatorErrorSchema" implements Serializable'); $t->is($e1->getGlobalErrors(), $e->getGlobalErrors(), '"sfValidatorErrorSchema" implements Serializable');
* This file is part of the symfony package. * (c) Fabien Potencier <*****@*****.**> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ require_once dirname(__FILE__) . '/../../bootstrap/unit.php'; $t = new lime_test(26, new lime_output_color()); $v1 = new sfValidatorString(); $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'); // ->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($v1, array($e1, 'e1' => $e1, 'e2' => $e2)); $es = new sfValidatorErrorSchema($v1, array($e1, 'e1' => $e1, 'e2' => $es1)); $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);