/** * 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 (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 WidgetSchema) { $class = 'Bundle\\sfFormBundle\\FieldSchema'; if ($error && !$error instanceof ValidatorErrorSchema) { $error = new ValidatorErrorSchema($error->getValidator(), array($error)); } } else { $class = 'Bundle\\sfFormBundle\\Field'; } $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 * * @throws \InvalidArgumentException when the widget not exist */ public function renderField($name, $value = null, $attributes = array(), $errors = array()) { if (null === ($widget = $this[$name])) { throw new \InvalidArgumentException(sprintf('The field named "%s" does not exist.', $name)); } if ($widget instanceof Schema && $errors && !$errors instanceof ErrorSchema) { $errors = new ErrorSchema($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); }
use Bundle\sfFormBundle\Widget\Schema as WidgetSchema; use Bundle\sfFormBundle\Widget\InputText as WidgetInputText; use Bundle\sfFormBundle\Widget\InputHidden as WidgetInputHidden; use Bundle\sfFormBundle\Validator\Error as ValidatorError; use Bundle\sfFormBundle\Validator\String as ValidatorString; use Bundle\sfFormBundle\Validator\ErrorSchema as ValidatorErrorSchema; $t = new lime_test(11); // widgets $authorSchema = new WidgetSchema(array('name' => $nameWidget = new WidgetInputText())); $authorSchema->setNameFormat('article[author][%s]'); $schema = new WidgetSchema(array('title' => $titleWidget = new WidgetInputText(), 'author' => $authorSchema)); $schema->setNameFormat('article[%s]'); // errors $authorErrorSchema = new ValidatorErrorSchema(new ValidatorString()); $authorErrorSchema->addError(new ValidatorError(new ValidatorString(), 'name error'), 'name'); $articleErrorSchema = new ValidatorErrorSchema(new ValidatorString()); $articleErrorSchema->addError($titleError = new ValidatorError(new ValidatorString(), 'title error'), 'title'); $articleErrorSchema->addError($authorErrorSchema, 'author'); $parent = new FieldSchema($schema, null, 'article', array('title' => 'symfony', 'author' => array('name' => 'Fabien')), $articleErrorSchema); $f = $parent['title']; $child = $parent['author']; // \ArrayAccess interface $t->diag('ArrayAccess interface'); $t->is(isset($parent['title']), true, 'sfFormField implements the \\ArrayAccess interface'); $t->is(isset($parent['title1']), false, 'sfFormField implements the \\ArrayAccess interface'); $t->is($parent['title'], $f, 'sfFormField implements the \\ArrayAccess interface'); try { unset($parent['title']); $t->fail('Field implements the \\ArrayAccess interface but in read-only mode'); } catch (\LogicException $e) { $t->pass('Field implements the \\ArrayAccess interface but in read-only mode');
$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 ErrorSchema(new String())); } $a = new NotSerializable(); try { $serialized = will_crash($a); $t->pass('ErrorSchema implements \\Serializable'); } catch (Exception $e) { $t->fail('ErrorSchema implements \\Serializable'); } $e = new ErrorSchema($v1); $e1 = unserialize($serialized); $t->is($e1->getMessage(), $e->getMessage(), 'ErrorSchema implements \\Serializable'); $t->is($e1->getCode(), $e->getCode(), 'ErrorSchema implements \\Serializable'); $t->is(get_class($e1->getValidator()), get_class($e->getValidator()), 'ErrorSchema implements \\Serializable'); $t->is($e1->getArguments(), $e->getArguments(), 'ErrorSchema implements \\Serializable'); $t->is($e1->getNamedErrors(), $e->getNamedErrors(), 'ErrorSchema implements \\Serializable'); $t->is($e1->getGlobalErrors(), $e->getGlobalErrors(), 'ErrorSchema implements \\Serializable');