Changeset 9498
- Timestamp:
- 06/09/08 19:35:50 (2 years ago)
- Files:
-
- branches/1.1/lib/form/sfForm.class.php (modified) (4 diffs)
- branches/1.1/test/unit/form/sfFormTest.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/lib/form/sfForm.class.php
r9438 r9498 254 254 public function embedForm($name, sfForm $form, $decorator = null) 255 255 { 256 if (true === $this->isBound() || true === $form->isBound()) 257 { 258 throw new LogicException('A bound form cannot be embedded'); 259 } 260 256 261 $form = clone $form; 257 262 unset($form[self::$CSRFFieldName]); … … 265 270 $this->widgetSchema[$name] = new sfWidgetFormSchemaDecorator($widgetSchema, $decorator); 266 271 $this->validatorSchema[$name] = $form->getValidatorSchema(); 267 272 268 273 $this->resetFormFields(); 269 274 } … … 283 288 public function embedFormForEach($name, sfForm $form, $n, $decorator = null, $innerDecorator = null, $attributes = array(), $options = array(), $labels = array()) 284 289 { 290 if (true === $this->isBound() || true === $form->isBound()) 291 { 292 throw new LogicException('A bound form cannot be embedded'); 293 } 294 285 295 $form = clone $form; 286 296 unset($form[self::$CSRFFieldName]); … … 307 317 $this->widgetSchema[$name] = new sfWidgetFormSchemaDecorator(new sfWidgetFormSchemaForEach(new sfWidgetFormSchemaDecorator($widgetSchema, $innerDecorator), $n, $attributes, $options, $labels), $decorator); 308 318 $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 } 309 367 310 368 $this->resetFormFields(); branches/1.1/test/unit/form/sfFormTest.php
r9438 r9498 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test( 89, new lime_output_color());13 $t = new lime_test(99, new lime_output_color()); 14 14 15 15 class FormTest extends sfForm … … 549 549 $t->ok($a1->getErrorSchema() !== $a->getErrorSchema(), '__clone() clones the error schema'); 550 550 $t->ok($a1->getErrorSchema()->getMessage() == $a->getErrorSchema()->getMessage(), '__clone() clones the error schema'); 551 552 // mergeForm() 553 $t->diag('mergeForm()'); 554 555 class 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 573 class 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 606 try 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 } 612 catch (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');

