Development

Changeset 7053

You must first sign up to be able to contribute.

Changeset 7053

Show
Ignore:
Timestamp:
01/14/08 19:57:07 (10 months ago)
Author:
fabien
Message:

refactored form/validators/widgets embedding

  • sfWidgetFormSchema clones embedded widgets
  • sfValidatorSchema clones embedded validators
  • sfForm::embedForm() and sfForm::embedForEach() name format argument has been removed (see below)
  • remove the constructor first argument (name format) for sfWidgetFormSchemaForEach
  • sfWidgetFormSchema name format is forced when a widget schema is embedded in another
    • added setParent() and getParent() (the parent widget is now remembered)
    • an exception is thrown if the name format does not contains %s
    • the widget name generation is now recursive
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/form/sfForm.class.php

    r7051 r7053  
    208208   * @param string The field name 
    209209   * @param sfForm A sfForm instance 
    210    * @param string The format to use for widget name 
    211210   * @param string A HTML decorator for the embedded form 
    212211   */ 
    213   public function embedForm($name, sfForm $form, $nameFormat = null, $decorator = null) 
    214   { 
    215     // change the name format for the embedded widget 
    216     if (is_null($nameFormat)) 
    217     { 
    218       $nameFormat = $this->generateNameFormatForEmbedded($name, $this->widgetSchema->getNameFormat()); 
    219     } 
    220  
     212  public function embedForm($name, sfForm $form, $decorator = null) 
     213  { 
    221214    $form = clone $form; 
    222215    unset($form[self::$CSRFFieldName]); 
    223216 
    224217    $widgetSchema = $form->getWidgetSchema(); 
    225     $widgetSchema->setNameFormat($nameFormat); 
    226218 
    227219    $this->setDefault($name, $form->getDefaults()); 
     
    240232   * @param string  The field name 
    241233   * @param sfForm  A sfForm instance 
    242    * @param integer The number of times to include the form 
    243    * @param string  The format to use for widget name 
     234   * @param integer The number of times to embed the form 
    244235   * @param string  A HTML decorator for the main form around embedded forms 
    245236   * @param string  A HTML decorator for each embedded form 
    246237   */ 
    247   public function embedFormForEach($name, sfForm $form, $n, $nameFormat = null, $decorator = null, $innerDecorator = null, $attributes = array(), $options = array(), $labels = array()) 
    248   { 
    249     // change the name format for the embedded widget 
    250     if (is_null($nameFormat)) 
    251     { 
    252       $nameFormat = $this->generateNameFormatForEmbedded($name, $this->widgetSchema->getNameFormat()); 
    253     } 
    254  
     238  public function embedFormForEach($name, sfForm $form, $n, $decorator = null, $innerDecorator = null, $attributes = array(), $options = array(), $labels = array()) 
     239  { 
    255240    $form = clone $form; 
    256241    unset($form[self::$CSRFFieldName]); 
     242 
     243    $widgetSchema = $form->getWidgetSchema(); 
    257244 
    258245    // generate labels and default values 
     
    262249      if (!isset($labels[$i])) 
    263250      { 
    264         $labels[$i] = sprintf('%s (%s)', $form->getWidgetSchema()->generateLabelName($name), $i); 
     251        $labels[$i] = sprintf('%s (%s)', $widgetSchema->generateLabelName($name), $i); 
    265252      } 
    266253 
     
    270257    $this->setDefault($name, $defaults); 
    271258 
    272     $decorator = is_null($decorator) ? $form->getWidgetSchema()->getFormFormatter()->getDecoratorFormat() : $decorator; 
    273     $innerDecorator = is_null($innerDecorator) ? $form->getWidgetSchema()->getFormFormatter()->getDecoratorFormat() : $innerDecorator; 
    274  
    275     $this->widgetSchema[$name] = new sfWidgetFormSchemaDecorator(new sfWidgetFormSchemaForEach($nameFormat, new sfWidgetFormSchemaDecorator($form->getWidgetSchema(), $innerDecorator), $n, $attributes, $options, $labels), $decorator); 
     259    $decorator = is_null($decorator) ? $widgetSchema->getFormFormatter()->getDecoratorFormat() : $decorator; 
     260    $innerDecorator = is_null($innerDecorator) ? $widgetSchema->getFormFormatter()->getDecoratorFormat() : $innerDecorator; 
     261 
     262    $this->widgetSchema[$name] = new sfWidgetFormSchemaDecorator(new sfWidgetFormSchemaForEach(new sfWidgetFormSchemaDecorator($widgetSchema, $innerDecorator), $n, $attributes, $options, $labels), $decorator); 
    276263    $this->validatorSchema[$name] = new sfValidatorSchemaForEach($form->getValidatorSchema(), $n); 
    277264 
     
    621608 
    622609    return $this->formField; 
    623   } 
    624  
    625   /** 
    626    * Generates a name format for embedded forms. 
    627    * 
    628    * @param  string The widget name 
    629    * @param  string The current name format 
    630    * 
    631    * @return string The name format to use for embedding 
    632    * 
    633    * @see embedFormForEach() 
    634    * @see embedForm() 
    635    */ 
    636   protected function generateNameFormatForEmbedded($name, $nameFormat) 
    637   { 
    638     // if current name format is something[%s], change it to something[$name][%s] 
    639     // else change it to $name[%s] 
    640     if ('[%s]' === substr($nameFormat, -4)) 
    641     { 
    642       return sprintf('%s[%s][%%s]', substr($nameFormat, 0, -4), $name); 
    643     } 
    644     else 
    645     { 
    646       return sprintf('%s[%%s]', $name); 
    647     } 
    648610  } 
    649611 
  • branches/1.1/lib/validator/sfValidatorSchema.class.php

    r7052 r7053  
    245245  public function setPreValidator(sfValidator $validator) 
    246246  { 
    247     $this->preValidator = $validator; 
     247    $this->preValidator = clone $validator; 
    248248  } 
    249249 
     
    265265  public function setPostValidator(sfValidator $validator) 
    266266  { 
    267     $this->postValidator = $validator; 
     267    $this->postValidator = clone $validator; 
    268268  } 
    269269 
     
    315315    } 
    316316 
    317     $this->fields[$name] = $validator; 
     317    $this->fields[$name] = clone $validator; 
    318318  } 
    319319 
  • branches/1.1/lib/widget/sfWidgetFormSchema.class.php

    r7051 r7053  
    2828 
    2929  protected 
     30    $parent         = null, 
    3031    $formFormatters = array(), 
    3132    $options        = array(), 
     
    5859  public function __construct($fields = null, $options = array(), $attributes = array(), $labels = array(), $helps = array()) 
    5960  { 
    60     if (is_array($fields)) 
    61     { 
    62       foreach ($fields as $name => $widget) 
    63       { 
    64         $this[$name] = $widget; 
    65       } 
    66     } 
    67     else if (!is_null($fields)) 
    68     { 
    69       throw new InvalidArgumentException('sfWidgetFormSchema constructor takes an array of sfWidget objects.'); 
    70     } 
    71  
    7261    $this->labels = $labels; 
    7362    $this->helps  = $helps; 
     
    7766 
    7867    parent::__construct($options, $attributes); 
     68 
     69    if (is_array($fields)) 
     70    { 
     71      foreach ($fields as $name => $widget) 
     72      { 
     73        $this[$name] = $widget; 
     74      } 
     75    } 
     76    else if (!is_null($fields)) 
     77    { 
     78      throw new InvalidArgumentException('sfWidgetFormSchema constructor takes an array of sfWidget objects.'); 
     79    } 
    7980  } 
    8081 
     
    152153  public function setNameFormat($format) 
    153154  { 
     155    if (false !== $format && false === strpos($format, '%s')) 
     156    { 
     157      throw new InvalidArgumentException(sprintf('The name format must contain %%s ("%s" given)', $format)); 
     158    } 
     159 
    154160    $this->options['name_format'] = $format; 
    155161  } 
     
    414420  public function generateName($name) 
    415421  { 
    416     if (false === $this->options['name_format']) 
    417     { 
    418       return $name; 
    419     } 
    420  
    421     if (false !== strpos($this->options['name_format'], '%s')) 
    422     { 
    423       return sprintf($this->options['name_format'], $name); 
     422    $format = $this->getNameFormat(); 
     423 
     424    if ('[%s]' == substr($format, -4)) 
     425    { 
     426      if (preg_match('/^(.+?)\[(.+)\]$/', $name, $match)) 
     427      { 
     428        $name = sprintf('%s[%s][%s]', substr($format, 0, -4), $match[1], $match[2]); 
     429      } 
     430      else 
     431      { 
     432        $name = sprintf('%s[%s]', substr($format, 0, -4), $name); 
     433      } 
     434    } 
     435    else if (false !== $format) 
     436    { 
     437      $name = sprintf($format, $name); 
     438    } 
     439 
     440    if ($parent = $this->getParent()) 
     441    { 
     442      $name = $parent->generateName($name); 
    424443    } 
    425444 
    426445    return $name; 
     446  } 
     447 
     448  /** 
     449   * Gets the parent widget schema. 
     450   * 
     451   * @return sfWidgetFormSchema The parent widget schema 
     452   */ 
     453  public function getParent() 
     454  { 
     455    return $this->parent; 
     456  } 
     457 
     458  /** 
     459   * Sets the parent widget schema. 
     460   * 
     461   * @parent sfWidgetFormSchema The parent widget schema 
     462   */ 
     463  public function setParent(sfWidgetFormSchema $parent = null) 
     464  { 
     465    $this->parent = $parent; 
    427466  } 
    428467 
     
    469508    } 
    470509 
    471     $this->fields[$name] = $widget; 
     510    $this->fields[$name] = clone $widget; 
     511 
     512    if ($widget instanceof sfWidgetFormSchema) 
     513    { 
     514      $this->fields[$name]->setParent($this); 
     515      $this->fields[$name]->setNameFormat($name.'[%s]'); 
     516    } 
    472517  } 
    473518 
     
    596641    foreach ($this->fields as $name => $field) 
    597642    { 
    598       $this->fields[$name] = clone $field; 
     643      // offsetSet will clone the field and change the parent 
     644      $this[$name] = $field; 
    599645    } 
    600646  } 
  • branches/1.1/lib/widget/sfWidgetFormSchemaDecorator.class.php

    r7046 r7053  
    228228   * @see sfWidgetFormSchema 
    229229   */ 
    230   public function setParent(sfWidgetFormSchema $parent
     230  public function setParent(sfWidgetFormSchema $parent = null
    231231  { 
    232232    $this->widget->setParent($parent); 
  • branches/1.1/lib/widget/sfWidgetFormSchemaForEach.class.php

    r5937 r7053  
    1010 
    1111/** 
    12  *  
     12 * sfWidgetFormSchemaForEach duplicates a given widget multiple times in a widget schema. 
    1313 * 
    1414 * @package    symfony 
     
    2222   * Constructor. 
    2323   * 
    24    * @param string             The name format string 
    2524   * @param sfWidgetFormSchema A sfWidgetFormSchema instance 
    26    * @param integer            The number of times to replicate the widget 
     25   * @param integer            The number of times to duplicate the widget 
    2726   * @param array              An array of options 
    2827   * @param array              An array of default HTML attributes 
     
    3130   * @see sfWidgetFormSchema 
    3231   */ 
    33   public function __construct($nameFormat, sfWidgetFormSchema $widget, $count, $options = array(), $attributes = array(), $labels = array()) 
     32  public function __construct(sfWidgetFormSchema $widget, $count, $options = array(), $attributes = array(), $labels = array()) 
    3433  { 
    35     $fields = array(); 
    36     for ($i = 0; $i < $count; $i++) 
    37     { 
    38       $clone = clone $widget; 
    39       $clone->setNameFormat(sprintf($nameFormat, $i).'[%s]'); 
    40  
    41       $fields[$i] = $clone; 
    42     } 
    43  
    44     parent::__construct($fields, $options, $attributes, $labels); 
     34    parent::__construct(array_fill(0, $count, $widget), $options, $attributes, $labels); 
    4535  } 
    4636} 
  • branches/1.1/test/unit/form/sfFormFieldTest.php

    r7047 r7053  
    3939// ->getValue() ->getWidget() ->getParent() ->getError() ->hasError() 
    4040$t->diag('->getValue() ->getWidget() ->getParent() ->getError() ->hasError()'); 
    41 $t->is($f->getWidget(), $titleWidget, '->getWidget() returns the form field widget'); 
     41$t->ok($f->getWidget() == $titleWidget, '->getWidget() returns the form field widget'); 
    4242$t->is($f->getValue(), 'symfony', '->getValue() returns the form field value'); 
    4343$t->is($f->getParent(), $parent, '->getParent() returns the form field parent'); 
  • branches/1.1/test/unit/form/sfFormTest.php

    r7051 r7053  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(82, new lime_output_color()); 
     13$t = new lime_test(83, new lime_output_color()); 
    1414 
    1515class FormTest extends sfForm 
     
    1818  { 
    1919    return "*$secret*"; 
    20   } 
    21  
    22   public function generateNameFormatForEmbedded($name, $nameFormat) 
    23   { 
    24     return parent::generateNameFormatForEmbedded($name, $nameFormat); 
    2520  } 
    2621} 
     
    117112$f->setValidators($validators); 
    118113$schema = $f->getValidatorSchema(); 
    119 $t->is_deeply($schema['first_name'], $validators['first_name'], '->setValidators() sets field validators'); 
    120 $t->is_deeply($schema['last_name'], $validators['last_name'], '->setValidators() sets field validators'); 
     114$t->ok($schema['first_name'] == $validators['first_name'], '->setValidators() sets field validators'); 
     115$t->ok($schema['last_name'] == $validators['last_name'], '->setValidators() sets field validators'); 
    121116 
    122117// ->setWidgets() ->setWidgetSchema() ->getWidgetSchema() 
     
    129124$widgetSchema = new sfWidgetFormSchema($widgets); 
    130125$f->setWidgetSchema($widgetSchema); 
    131 $t->is_deeply($f->getWidgetSchema(), $widgetSchema, '->setWidgetSchema() sets the current widget schema'); 
     126$t->ok($f->getWidgetSchema() == $widgetSchema, '->setWidgetSchema() sets the current widget schema'); 
    132127$f->setWidgets($widgets); 
    133128$schema = $f->getWidgetSchema(); 
    134 $t->is_deeply($schema['first_name'], $widgets['first_name'], '->setWidgets() sets field widgets'); 
    135 $t->is_deeply($schema['last_name'], $widgets['last_name'], '->setWidgets() sets field widgets'); 
     129$t->ok($schema['first_name'] == $widgets['first_name'], '->setWidgets() sets field widgets'); 
     130$t->ok($schema['last_name'] == $widgets['last_name'], '->setWidgets() sets field widgets'); 
    136131 
    137132// ArrayAccess interface 
     
    220215$t->is($f->getErrorSchema()->getCode(), '1 [min_length] file [max_size]', '->bind() behaves correctly with files'); 
    221216 
    222 // ->generateNameFormatForEmbedded() 
    223 $t->diag('->generateNameFormatForEmbedded()'); 
    224 $f = new FormTest(); 
    225 $t->is($f->generateNameFormatForEmbedded('article', '%s'), 'article[%s]', '->generateNameFormatForEmbedded() generates a name format for an embed form'); 
    226 $t->is($f->generateNameFormatForEmbedded('author', 'article[%s]'), 'article[author][%s]', '->generateNameFormatForEmbedded() generates a name format for an embed form'); 
    227  
    228217// ->embedForm() 
    229218$t->diag('->embedForm()'); 
     219 
    230220$author = new FormTest(array('first_name' => 'Fabien')); 
    231221$author->setWidgetSchema($author_widget_schema = new sfWidgetFormSchema(array('first_name' => new sfWidgetFormInput()))); 
    232222$author->setValidatorSchema($author_validator_schema = new sfValidatorSchema(array('first_name' => new sfValidatorString(array('min_length' => 2))))); 
     223 
     224$company = new FormTest(); 
     225$company->setWidgetSchema($company_widget_schema = new sfWidgetFormSchema(array('name' => new sfWidgetFormInput()))); 
     226$company->setValidatorSchema($company_validator_schema = new sfValidatorSchema(array('name' => new sfValidatorString(array('min_length' => 2))))); 
     227 
    233228$article = new FormTest(); 
    234229$article->setWidgetSchema($article_widget_schema = new sfWidgetFormSchema(array('title' => new sfWidgetFormInput()))); 
    235230$article->setValidatorSchema($article_validator_schema = new sfValidatorSchema(array('title' => new sfValidatorString(array('min_length' => 2))))); 
    236231 
     232$author->embedForm('company', $company); 
    237233$article->embedForm('author', $author); 
    238234$v = $article->getValidatorSchema(); 
    239235$w = $article->getWidgetSchema(); 
    240236$d = $article->getDefaults(); 
     237 
     238$w->setNameFormat('article[%s]'); 
    241239 
    242240$t->ok($v['author']['first_name'] == $author_validator_schema['first_name'], '->embedForm() embeds the validator schema'); 
     
    245243$t->is($v['author'][sfForm::getCSRFFieldName()], null, '->embedForm() removes the CSRF token for the embedded form'); 
    246244$t->is($w['author'][sfForm::getCSRFFieldName()], null, '->embedForm() removes the CSRF token for the embedded form'); 
     245 
     246$t->is($w['author']->generateName('first_name'), 'article[author][first_name]', '->embedForm() changes the name format to reflect the embedding'); 
     247$t->is($w['author']['company']->generateName('name'), 'article[author][company][name]', '->embedForm() changes the name format to reflect the embedding'); 
    247248 
    248249// ->embedFormForEach() 
     
    252253$w = $article->getWidgetSchema(); 
    253254$d = $article->getDefaults(); 
     255$w->setNameFormat('article[%s]'); 
    254256 
    255257for ($i = 0; $i < 2; $i++) 
     
    261263  $t->is($w['authors'][$i][sfForm::getCSRFFieldName()], null, '->embedFormForEach() removes the CSRF token for the embedded forms'); 
    262264} 
     265 
     266$t->is($w['authors'][0]->generateName('first_name'), 'article[authors][0][first_name]', '->embedFormForEach() changes the name format to reflect the embedding'); 
    263267 
    264268// ::convertFileInformation() 
  • branches/1.1/test/unit/validator/sfValidatorSchemaTest.php

    r7052 r7053  
    8888 
    8989$v = new sfValidatorSchema(array('s1' => $v1)); 
    90 $t->is($v['s1'], $v1, 'sfValidatorSchema implements the ArrayAccess interface for the fields'); 
     90$t->ok($v['s1'] == $v1, 'sfValidatorSchema implements the ArrayAccess interface for the fields'); 
    9191$t->is($v['s2'], null, 'sfValidatorSchema implements the ArrayAccess interface for the fields'); 
    9292 
     
    161161$v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2)); 
    162162$v->setPreValidator($preValidator = new PreValidator()); 
    163 $t->is($v->getPreValidator(), $preValidator, '->getPreValidator() returns the current pre validator'); 
     163$t->ok($v->getPreValidator() == $preValidator, '->getPreValidator() returns the current pre validator'); 
    164164try 
    165165{ 
     
    181181$v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2)); 
    182182$v->setPostValidator($postValidator = new PostValidator()); 
    183 $t->is($v->getPostValidator(), $postValidator, '->getPostValidator() returns the current post validator'); 
     183$t->ok($v->getPostValidator() == $postValidator, '->getPostValidator() returns the current post validator'); 
    184184$t->is($v->clean(array('s1' => 'foo', 's2' => 'bar')), array('s1' => '*foo*', 's2' => '*bar*'), '->clean() executes post validators'); 
    185185 
     
    221221 
    222222$t->diag('one validator fails'); 
    223 $v2->setOption('max_length', 2); 
     223$v['s2']->setOption('max_length', 2); 
    224224try 
    225225{ 
     
    236236 
    237237$t->diag('several validators fail'); 
    238 $v1->setOption('max_length', 2); 
    239 $v2->setOption('max_length', 2); 
     238$v['s1']->setOption('max_length', 2); 
     239$v['s2']->setOption('max_length', 2); 
    240240try 
    241241{ 
     
    269269 
    270270$t->diag('postValidator throws global errors'); 
    271 $comparator->setOption('throw_global_error', true); 
     271foreach (array($userValidator->getPostValidator(), $v->getPostValidator(), $v['embedded']->getPostValidator()) as $validator) 
     272
     273  $validator->setOption('throw_global_error', true); 
     274
    272275try 
    273276{ 
     
    287290 
    288291$t->diag('postValidator throws named errors'); 
    289 $comparator->setOption('throw_global_error', false); 
     292foreach (array($userValidator->getPostValidator(), $v->getPostValidator(), $v['embedded']->getPostValidator()) as $validator) 
     293
     294  $validator->setOption('throw_global_error', false); 
     295
    290296try 
    291297{ 
  • branches/1.1/test/unit/widget/sfWidgetFormSchemaDecoratorTest.php

    r7051 r7053  
    5858 
    5959$w = new sfWidgetFormSchemaDecorator($ws, "<table>\n%content%</table>"); 
    60 $t->is($w['w1'], $w1, 'sfWidgetFormSchemaDecorator implements the ArrayAccess interface for the fields'); 
    61 $t->is($w['w2'], $w2, 'sfWidgetFormSchemaDecorator implements the ArrayAccess interface for the fields'); 
    62 $t->is($ws['w1'], $w1, 'sfWidgetFormSchemaDecorator implements the ArrayAccess interface for the fields'); 
    63 $t->is($ws['w2'], $w2, 'sfWidgetFormSchemaDecorator implements the ArrayAccess interface for the fields'); 
     60$t->ok($w['w1'] == $w1, 'sfWidgetFormSchemaDecorator implements the ArrayAccess interface for the fields'); 
     61$t->ok($w['w2'] == $w2, 'sfWidgetFormSchemaDecorator implements the ArrayAccess interface for the fields'); 
     62$t->ok($ws['w1'] == $w1, 'sfWidgetFormSchemaDecorator implements the ArrayAccess interface for the fields'); 
     63$t->ok($ws['w2'] == $w2, 'sfWidgetFormSchemaDecorator implements the ArrayAccess interface for the fields'); 
    6464 
    6565$w = new sfWidgetFormSchemaDecorator($ws, "<table>\n%content%</table>"); 
  • branches/1.1/test/unit/widget/sfWidgetFormSchemaForEachTest.php

    r5937 r7053  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(1, new lime_output_color()); 
     13$t = new lime_test(4, new lime_output_color()); 
    1414 
    1515$w1 = new sfWidgetFormInput(); 
     
    1818// __construct() 
    1919$t->diag('__construct()'); 
    20 $wf = new sfWidgetFormSchemaForEach('article[%s]', $w, 2); 
    21 $wc1 = clone $w; 
    22 $wc1->setNameFormat('article[0][%s]'); 
    23 $wc2 = clone $w; 
    24 $wc2->setNameFormat('article[1][%s]'); 
    25 $t->is($wf->getFields(), array($wc1, $wc2), '__construct() takes a sfWidgetFormSchema as its second argument'); 
     20$wf = new sfWidgetFormSchemaForEach($w, 2); 
     21$t->ok($wf[0]['w1'] !== $w1, '__construct() takes a sfWidgetFormSchema as its first argument'); 
     22$t->ok($wf[0]['w1'] == $w1, '__construct() takes a sfWidgetFormSchema as its first argument'); 
     23$t->ok($wf[1]['w1'] !== $w1, '__construct() takes a sfWidgetFormSchema as its first argument'); 
     24$t->ok($wf[1]['w1'] == $w1, '__construct() takes a sfWidgetFormSchema as its first argument'); 
  • branches/1.1/test/unit/widget/sfWidgetFormSchemaTest.php

    r7051 r7053  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(55, new lime_output_color()); 
     13$t = new lime_test(59, new lime_output_color()); 
    1414 
    1515$w1 = new sfWidgetFormInput(array(), array('class' => 'foo1')); 
     
    6262 
    6363$w = new sfWidgetFormSchema(array('w1' => $w1)); 
    64 $t->is($w['w1'], $w1, 'sfWidgetFormSchema implements the ArrayAccess interface for the fields'); 
     64$t->ok($w['w1'] == $w1, 'sfWidgetFormSchema implements the ArrayAccess interface for the fields'); 
    6565$t->is($w['w2'], null, 'sfWidgetFormSchema implements the ArrayAccess interface for the fields'); 
    6666 
     
    103103$t->is($w->getNameFormat(), 'article[%s]', '->getNameFormat() returns the name format'); 
    104104 
    105 $w->setNameFormat('foo'); 
    106 $t->is($w->generateName('foo'), 'foo', '->generateName() returns the name unchanged if the format does not contain %s'); 
    107  
    108105$w->setNameFormat(false); 
    109106$t->is($w->generateName('foo'), 'foo', '->generateName() returns the name unchanged if the format is false'); 
     107 
     108try 
     109{ 
     110  $w->setNameFormat('foo'); 
     111  $t->fail('->setNameFormat() throws an InvalidArgumentException if the format does not contain %s'); 
     112} 
     113catch (InvalidArgumentException $e) 
     114{ 
     115  $t->pass('->setNameFormat() throws an InvalidArgumentException if the format does not contain %s'); 
     116} 
     117 
     118$w = new sfWidgetFormSchema(array( 
     119  'author' => new sfWidgetFormSchema(array( 
     120    'first_name' => new sfWidgetFormInput(), 
     121    'company'    => new sfWidgetFormSchema(array( 
     122      'name' => new sfWidgetFormInput(), 
     123    )), 
     124  )), 
     125)); 
     126$w->setNameFormat('article[%s]'); 
     127$t->is($w['author']->generateName('first_name'), 'article[author][first_name]', '->generateName() returns a HTML name attribute value for a given field name'); 
     128$t->is($w['author']['company']->generateName('name'), 'article[author][company][name]', '->generateName() returns a HTML name attribute value for a given field name'); 
     129 
     130// ->getParent() ->setParent() 
     131$t->diag('->getParent() ->setParent()'); 
     132$author = new sfWidgetFormSchema(array('first_name' => new sfWidgetFormInput())); 
     133$company = new sfWidgetFormSchema(array('name' => new sfWidgetFormInput())); 
     134$t->is($company->getParent(), null, '->getParent() returns null if there is no parent widget schema'); 
     135$company->setParent($author); 
     136$t->is($company->getParent(), $author, '->getParent() returns the parent widget schema'); 
    110137 
    111138// ->setLabels() ->setLabel() ->getLabels() ->getLabel() ->generateLabelName()