Development

Changeset 9498

You must first sign up to be able to contribute.

Changeset 9498

Show
Ignore:
Timestamp:
06/09/08 19:35:50 (2 years ago)
Author:
nicolas
Message:

Added $sfForm::mergeForm(sfForm $form) method which allows to merge two existing form instances, including:

  • widgets
  • validators
  • pre and post validators
  • elements of the form passed as argument will override the existing ones by default
  • bound forms cannot be merged (this behavior has been replicated in the embedForm and embedFormForEach methods)
Files:

Legend:

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

    r9438 r9498  
    254254  public function embedForm($name, sfForm $form, $decorator = null) 
    255255  { 
     256    if (true === $this->isBound() || true === $form->isBound()) 
     257    { 
     258      throw new LogicException('A bound form cannot be embedded'); 
     259    } 
     260     
    256261    $form = clone $form; 
    257262    unset($form[self::$CSRFFieldName]); 
     
    265270    $this->widgetSchema[$name] = new sfWidgetFormSchemaDecorator($widgetSchema, $decorator); 
    266271    $this->validatorSchema[$name] = $form->getValidatorSchema(); 
    267  
     272     
    268273    $this->resetFormFields(); 
    269274  } 
     
    283288  public function embedFormForEach($name, sfForm $form, $n, $decorator = null, $innerDecorator = null, $attributes = array(), $options = array(), $labels = array()) 
    284289  { 
     290    if (true === $this->isBound() || true === $form->isBound()) 
     291    { 
     292      throw new LogicException('A bound form cannot be embedded'); 
     293    } 
     294     
    285295    $form = clone $form; 
    286296    unset($form[self::$CSRFFieldName]); 
     
    307317    $this->widgetSchema[$name] = new sfWidgetFormSchemaDecorator(new sfWidgetFormSchemaForEach(new sfWidgetFormSchemaDecorator($widgetSchema, $innerDecorator), $n, $attributes, $options, $labels), $decorator); 
    308318    $this->validatorSchema[$name] = new sfValidatorSchemaForEach($form->getValidatorSchema(), $n); 
     319 
     320    $this->resetFormFields(); 
     321  } 
     322   
     323  /** 
     324   * Merges current form widget and validator schemas with the ones from the  
     325   * sfForm object passed as parameter 
     326   * 
     327   * @param  sfForm   $form      The sfForm instance to merge with current form 
     328   * @throws LogicException      If one of the form has already been bound 
     329   */ 
     330  public function mergeForm(sfForm $form) 
     331  { 
     332    if (true === $this->isBound() || true === $form->isBound()) 
     333    { 
     334      throw new LogicException('A bound form cannot be merged'); 
     335    } 
     336 
     337    $form = clone $form; 
     338    unset($form[self::$CSRFFieldName]); 
     339 
     340    $this->defaults = array_merge($this->defaults, $form->getDefaults()); 
     341 
     342    foreach ($form->getWidgetSchema()->getFields() as $field => $widget) 
     343    { 
     344      $this->widgetSchema[$field] = $widget; 
     345    } 
     346     
     347    foreach ($form->getValidatorSchema()->getFields() as $field => $validator) 
     348    { 
     349      $this->validatorSchema[$field] = $validator; 
     350    } 
     351     
     352    if (!is_null($form->getValidatorSchema()->getPreValidator())) 
     353    { 
     354      $this->validatorSchema->setPreValidator(new sfValidatorAnd(array( 
     355        !is_null($this->validatorSchema->getPreValidator()) ? $this->validatorSchema->getPreValidator() : new sfValidatorPass(),  
     356        $form->getValidatorSchema()->getPreValidator(), 
     357      ))); 
     358    } 
     359     
     360    if (!is_null($form->getValidatorSchema()->getPostValidator())) 
     361    { 
     362      $this->validatorSchema->setPostValidator(new sfValidatorAnd(array( 
     363        !is_null($this->validatorSchema->getPostValidator()) ? $this->validatorSchema->getPostValidator() : new sfValidatorPass(),  
     364        $form->getValidatorSchema()->getPostValidator(), 
     365      ))); 
     366    } 
    309367 
    310368    $this->resetFormFields(); 
  • branches/1.1/test/unit/form/sfFormTest.php

    r9438 r9498  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(89, new lime_output_color()); 
     13$t = new lime_test(99, new lime_output_color()); 
    1414 
    1515class FormTest extends sfForm 
     
    549549$t->ok($a1->getErrorSchema() !== $a->getErrorSchema(), '__clone() clones the error schema'); 
    550550$t->ok($a1->getErrorSchema()->getMessage() == $a->getErrorSchema()->getMessage(), '__clone() clones the error schema'); 
     551 
     552// mergeForm() 
     553$t->diag('mergeForm()'); 
     554 
     555class TestForm1 extends FormTest 
     556{ 
     557  public function configure() 
     558  { 
     559    $this->disableCSRFProtection(); 
     560    $this->setWidgets(array( 
     561      'a' => new sfWidgetFormInput(), 
     562      'b' => new sfWidgetFormInput(), 
     563      'c' => new sfWidgetFormInput(), 
     564    )); 
     565    $this->setValidators(array( 
     566      'a' => new sfValidatorString(array('min_length' => 2)), 
     567      'b' => new sfValidatorString(array('max_length' => 3)), 
     568      'c' => new sfValidatorString(array('max_length' => 1000)), 
     569    )); 
     570  } 
     571} 
     572 
     573class TestForm2 extends FormTest 
     574{ 
     575  public function configure() 
     576  { 
     577    $this->disableCSRFProtection(); 
     578    $this->setWidgets(array( 
     579      'c' => new sfWidgetFormTextarea(), 
     580      'd' => new sfWidgetFormTextarea(), 
     581    )); 
     582    $this->setValidators(array( 
     583      'c' => new sfValidatorPass(), 
     584      'd' => new sfValidatorString(array('max_length' => 5)), 
     585    )); 
     586    $this->validatorSchema->setPreValidator(new sfValidatorPass()); 
     587    $this->validatorSchema->setPostValidator(new sfValidatorPass()); 
     588  } 
     589} 
     590 
     591$f1 = new TestForm1(); 
     592$f2 = new TestForm2(); 
     593$f1->mergeForm($f2); 
     594 
     595$widgetSchema = $f1->getWidgetSchema(); 
     596$validatorSchema = $f1->getValidatorSchema(); 
     597$t->is(count($widgetSchema->getFields()), 4, 'mergeForm() merges a widget form schema'); 
     598$t->is(count($validatorSchema->getFields()), 4, 'mergeForm() merges a validator schema'); 
     599$t->is(array_keys($widgetSchema->getFields()), array('a', 'b', 'c', 'd'), 'mergeForms() merges the correct widgets'); 
     600$t->is(array_keys($validatorSchema->getFields()), array('a', 'b', 'c', 'd'), 'mergeForms() merges the correct validators'); 
     601$t->isa_ok($widgetSchema['c'], 'sfWidgetFormTextarea', 'mergeForm() overrides original form widget'); 
     602$t->isa_ok($validatorSchema['c'], 'sfValidatorPass', 'mergeForm() overrides original form validator'); 
     603$t->isa_ok($validatorSchema->getPreValidator(), 'sfValidatorAnd', 'mergeForm() merges pre validator'); 
     604$t->isa_ok($validatorSchema->getPostValidator(), 'sfValidatorAnd', 'mergeForm() merges post validator'); 
     605 
     606try 
     607{ 
     608  $f1->bind(array('a' => 'foo', 'b' => 'bar', 'd' => 'far_too_long_value')); 
     609  $f1->mergeForm($f2); 
     610  $t->fail('mergeForm() disallows merging already bound forms'); 
     611} 
     612catch (LogicException $e) 
     613{ 
     614  $t->pass('mergeForm() disallows merging already bound forms'); 
     615} 
     616 
     617$errorSchema = $f1->getErrorSchema(); 
     618$t->ok(array_key_exists('d', $errorSchema->getErrors()), 'mergeForm() merges errors after having been bound'); 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting.
Sensio Labs also supports several large Open-Source projects.