/** * Setup the form. To overload, you should use ->configure() */ public function setup() { $widgetSchema = new sfWidgetFormSchema(array('query' => new sfWidgetFormInput()), array(), array(), array(), array()); $widgetSchema->addFormFormatter('sfLuceneSimple', new sfLuceneWidgetFormatterSimple($widgetSchema)); $widgetSchema->setFormFormatterName('sfLuceneSimple'); $widgetSchema->setNameFormat('form[%s]'); $validatorSchema = new sfValidatorSchema(array('query' => new sfValidatorString(array('required' => true)), 'page' => new sfValidatorInteger(array('required' => false, 'empty_value' => 1))), array(), array()); if ($this->hasCategories()) { $widgetSchema['category'] = new sfWidgetFormSelect(array('choices' => $this->getCategories(), 'multiple' => false)); $validatorSchema['category'] = new sfValidatorChoice(array('required' => false, 'choices' => $this->getCategories())); } $this->setWidgetSchema($widgetSchema); $this->setValidatorSchema($validatorSchema); }
/** * Setup the form. To overload, you should use ->configure() */ public function setup() { $widgetSchema = new sfWidgetFormSchema(array('keywords' => new sfWidgetFormInput(), 'musthave' => new sfWidgetFormInput(), 'mustnothave' => new sfWidgetFormInput(), 'hasphrase' => new sfWidgetFormInput()), array(), array(), array('keywords' => 'May contain keywords', 'musthave' => 'Must contain keywords', 'mustnothave' => 'Must exclude keywords', 'hasphrase' => 'Contains exact phrase'), array()); $widgetSchema->addFormFormatter('sfLuceneAdvanced', new sfLuceneWidgetFormatterAdvanced($widgetSchema)); $widgetSchema->setFormFormatterName('sfLuceneAdvanced'); $widgetSchema->setNameFormat('form[%s]'); $validatorSchema = new sfValidatorSchema(array('keywords' => new sfValidatorString(array('required' => false)), 'musthave' => new sfValidatorString(array('required' => false)), 'mustnothave' => new sfValidatorString(array('required' => false)), 'hasphrase' => new sfValidatorString(array('required' => false))), array(), array()); if ($this->hasCategories()) { $widgetSchema['category'] = new sfWidgetFormSelect(array('choices' => $this->getCategories(), 'multiple' => false)); $widgetSchema->setLabel('category', 'Must be in category'); $validatorSchema['category'] = new sfValidatorChoice(array('required' => false, 'choices' => $this->getCategories())); } $this->setWidgetSchema($widgetSchema); $this->setValidatorSchema($validatorSchema); }
<?php /* * 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(31); // widgets $authorSchema = new sfWidgetFormSchema(array('id' => new sfWidgetFormInputHidden(), 'name' => $nameWidget = new sfWidgetFormInputText())); $authorSchema->setNameFormat('article[author][%s]'); $schema = new sfWidgetFormSchema(array('title' => $titleWidget = new sfWidgetFormInputText(), 'author' => $authorSchema)); $schema->setNameFormat('article[%s]'); // errors $authorErrorSchema = new sfValidatorErrorSchema(new sfValidatorString()); $authorErrorSchema->addError(new sfValidatorError(new sfValidatorString(), 'name error'), 'name'); $articleErrorSchema = new sfValidatorErrorSchema(new sfValidatorString()); $articleErrorSchema->addError($titleError = new sfValidatorError(new sfValidatorString(), 'title error'), 'title'); $articleErrorSchema->addError($authorErrorSchema, 'author'); $parent = new sfFormFieldSchema($schema, null, 'article', array('title' => 'symfony', 'author' => array('name' => 'Fabien')), $articleErrorSchema); $f = $parent['title']; $child = $parent['author']; // ->getValue() ->getWidget() ->getParent() ->getError() ->hasError() $t->diag('->getValue() ->getName() ->getWidget() ->getParent() ->getError() ->hasError()'); $t->ok($f->getWidget() == $titleWidget, '->getWidget() returns the form field widget'); $t->is($f->getName(), 'title', '->getName() returns the form field name'); $t->is($f->getValue(), 'symfony', '->getValue() returns the form field value'); $t->is($f->getParent(), $parent, '->getParent() returns the form field parent');
$errors = array('global error', 'w1' => 'error for w1', 'w2' => 'error for w2', 'w4' => array('w1' => 'error for w4/w1', 'w2' => 'error for w4/w2', 'w3' => 'error for w4/w3'), 'w4' => 'error for w4'); $t->is($w->getGlobalErrors($errors), array('global error', 'error for w4', 'W2' => 'error for w2'), '->getGlobalErrors() returns an array of global errors, errors for hidden fields, and errors for non existent fields'); // ->render() $t->diag('->render()'); $w = new sfWidgetFormSchema(); try { $w->render(null, 'string'); $t->fail('->render() throws an InvalidArgumentException if the second argument is not an array'); } catch (InvalidArgumentException $e) { $t->pass('->render() throws an InvalidArgumentException if the second argument is not an array'); } $w['first_name'] = $w1; $w['last_name'] = $w2; $w['id'] = new sfWidgetFormInputHidden(); $w->setAttribute('style', 'padding: 5px'); $w->setNameFormat('article[%s]'); $w->setIdFormat('id_%s'); $expected = <<<EOF <tr><td colspan="2"> <ul class="error_list"> <li>Global error message</li> <li>Id: Required</li> </ul> </td></tr> <tr> <th><label style="padding: 5px" for="id_article_first_name">First name</label></th> <td> <ul class="error_list"> <li>Too short</li> </ul> <input class="foo" type="text" name="article[first_name]" value="Fabien" id="id_article_first_name" /></td> </tr>
<?php /* * 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(31); // widgets $authorSchema = new sfWidgetFormSchema(array('id' => new sfWidgetFormInputHidden(), 'name' => $nameWidget = new sfWidgetFormInputText())); $authorSchema->setNameFormat('article[author][%s]'); $schema = new sfWidgetFormSchema(array('title' => $titleWidget = new sfWidgetFormInputText(), 'author' => $authorSchema)); $schema->setNameFormat('article[%s]'); $titleWidget->setParent($schema); // errors $authorErrorSchema = new sfValidatorErrorSchema(new sfValidatorString()); $authorErrorSchema->addError(new sfValidatorError(new sfValidatorString(), 'name error'), 'name'); $articleErrorSchema = new sfValidatorErrorSchema(new sfValidatorString()); $articleErrorSchema->addError($titleError = new sfValidatorError(new sfValidatorString(), 'title error'), 'title'); $articleErrorSchema->addError($authorErrorSchema, 'author'); $parent = new sfFormFieldSchema($schema, null, 'article', array('title' => 'symfony', 'author' => array('name' => 'Fabien')), $articleErrorSchema); $f = $parent['title']; $child = $parent['author']; // ->getValue() ->getWidget() ->getParent() ->getError() ->hasError() $t->diag('->getValue() ->getName() ->getWidget() ->getParent() ->getError() ->hasError()'); $t->ok($f->getWidget() == $titleWidget, '->getWidget() returns the form field widget'); $t->is($f->getName(), 'title', '->getName() returns the form field name'); $t->is($f->getValue(), 'symfony', '->getValue() returns the form field value');