This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/tests/Symfony/Tests/Component/Form/RepeatedFieldTest.php
Bernhard Schussek fb1f99137d [Form] Changed semantics of a "bound" form
A form now always has to be bound, independent of whether the request is a POST request or not. The bind() method detects itself whether the request was a post request or not and reads its data accordingly. The "old" bind()/isBound() methods were renamed to submit()/isSubmitted().

	$form = new Form('author');
	$form->bind($request, $author);

	if ($form->isValid()) {
		// isValid() implies isSubmitted(), non-submitted forms can
		// never be valid
		// do something with author now
	}

Alternatively, you can only bind global variables, if you don't have a request object.

	$form->bindGlobals($author);

Note that the $author object is in both cases optional. You can also pass no object at all and read the data using $form->getData(), but then no validation will occur. You can also prefill the form with an object during instantiation.

	$form = new Form('author', array('data' => $author));
	$form->bind($request);

	// etc.
2011-02-01 15:27:12 +01:00

61 lines
1.8 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Tests\Component\Form;
require_once __DIR__ . '/Fixtures/TestField.php';
use Symfony\Component\Form\RepeatedField;
use Symfony\Tests\Component\Form\Fixtures\TestField;
class RepeatedFieldTest extends \PHPUnit_Framework_TestCase
{
protected $field;
protected function setUp()
{
$this->field = new RepeatedField(new TestField('name'));
}
public function testSetData()
{
$this->field->setData('foobar');
$this->assertEquals('foobar', $this->field['first']->getData());
$this->assertEquals('foobar', $this->field['second']->getData());
}
public function testSubmitUnequal()
{
$input = array('first' => 'foo', 'second' => 'bar');
$this->field->submit($input);
$this->assertEquals('foo', $this->field['first']->getDisplayedData());
$this->assertEquals('bar', $this->field['second']->getDisplayedData());
$this->assertFalse($this->field->isFirstEqualToSecond());
$this->assertEquals($input, $this->field->getDisplayedData());
$this->assertEquals(null, $this->field->getData());
}
public function testSubmitEqual()
{
$input = array('first' => 'foo', 'second' => 'foo');
$this->field->submit($input);
$this->assertEquals('foo', $this->field['first']->getDisplayedData());
$this->assertEquals('foo', $this->field['second']->getDisplayedData());
$this->assertTrue($this->field->isFirstEqualToSecond());
$this->assertEquals($input, $this->field->getDisplayedData());
$this->assertEquals('foo', $this->field->getData());
}
}