Skip to content

Commit

Permalink
Fixing input generation for collections when prefixing table name
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Feb 27, 2014
1 parent c0faabb commit 0119f79
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
5 changes: 5 additions & 0 deletions src/View/Form/EntityContext.php
Expand Up @@ -217,6 +217,11 @@ protected function _getEntity($path) {
} }


$lastProp = $this->_rootName; $lastProp = $this->_rootName;

if ($path[0] === $this->_rootName) {
$path = array_slice($path, 1);
}

foreach ($path as $prop) { foreach ($path as $prop) {
$next = $this->_getProp($entity, $prop); $next = $this->_getProp($entity, $prop);
if ( if (
Expand Down
28 changes: 28 additions & 0 deletions tests/TestCase/View/Form/EntityContextTest.php
Expand Up @@ -230,6 +230,34 @@ public function testValOnCollections($collection) {
$this->assertNull($context->val('99.title')); $this->assertNull($context->val('99.title'));
} }


/**
* Test operations on a collection of entities when prefixing with the
* table name
*
* @dataProvider collectionProvider
* @return void
*/
public function testValOnCollectionsWithRootName($collection) {
$context = new EntityContext($this->request, [
'entity' => $collection,
'table' => 'Articles',
]);

$result = $context->val('Articles.0.title');
$this->assertEquals('First post', $result);

$result = $context->val('Articles.0.user.username');
$this->assertEquals('mark', $result);

$result = $context->val('Articles.1.title');
$this->assertEquals('Second post', $result);

$result = $context->val('Articles.1.user.username');
$this->assertEquals('jose', $result);

$this->assertNull($context->val('Articles.99.title'));
}

/** /**
* Test error operations on a collection of entities. * Test error operations on a collection of entities.
* *
Expand Down
29 changes: 13 additions & 16 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -2030,13 +2030,10 @@ public function testEmptyInputErrorValidation() {
* @return void * @return void
*/ */
public function testFormValidationAssociated() { public function testFormValidationAssociated() {
TableRegistry::get('Contacts', [
'className' => __NAMESPACE__ . '\ContactsTable'
]);
$nested = new Entity(['foo' => 'bar']); $nested = new Entity(['foo' => 'bar']);
$nested->errors('foo', ['not a valid bar']); $nested->errors('foo', ['not a valid bar']);
$entity = new Entity(['nested' => $nested]); $entity = new Entity(['nested' => $nested]);
$this->Form->create($entity, ['context' => ['table' => 'Contacts']]); $this->Form->create($entity);


$result = $this->Form->error('nested.foo'); $result = $this->Form->error('nested.foo');
$this->assertEquals('<div class="error-message">not a valid bar</div>', $result); $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
Expand All @@ -2050,14 +2047,11 @@ public function testFormValidationAssociated() {
* @return void * @return void
*/ */
public function testFormValidationAssociatedSecondLevel() { public function testFormValidationAssociatedSecondLevel() {
TableRegistry::get('Contacts', [
'className' => __NAMESPACE__ . '\ContactsTable'
]);
$inner = new Entity(['bar' => 'baz']); $inner = new Entity(['bar' => 'baz']);
$nested = new Entity(['foo' => $inner]); $nested = new Entity(['foo' => $inner]);
$entity = new Entity(['nested' => $nested]); $entity = new Entity(['nested' => $nested]);
$inner->errors('bar', ['not a valid one']); $inner->errors('bar', ['not a valid one']);
$this->Form->create($entity, ['context' => ['table' => 'Contacts']]); $this->Form->create($entity);
$result = $this->Form->error('nested.foo.bar'); $result = $this->Form->error('nested.foo.bar');
$this->assertEquals('<div class="error-message">not a valid one</div>', $result); $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
} }
Expand All @@ -2070,22 +2064,25 @@ public function testFormValidationAssociatedSecondLevel() {
* @return void * @return void
*/ */
public function testFormValidationMultiRecord() { public function testFormValidationMultiRecord() {
$this->markTestIncomplete('Need to revisit once models work again.'); $one = new Entity;
$Contact->validationErrors[2] = array( $two = new Entity;
'name' => array('The provided value is invalid') TableRegistry::get('Contacts', [
); 'className' => __NAMESPACE__ . '\ContactsTable'
$result = $this->Form->input('Contact.2.name'); ]);
$two->errors('name', ['This is wrong']);
$this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
$result = $this->Form->input('Contacts.1.name');
$expected = array( $expected = array(
'div' => array('class' => 'input text error'), 'div' => array('class' => 'input text error'),
'label' => array('for' => 'Contact2Name'), 'label' => array('for' => 'contacts-1-name'),
'Name', 'Name',
'/label', '/label',
'input' => array( 'input' => array(
'type' => 'text', 'name' => 'Contact[2][name]', 'id' => 'Contact2Name', 'type' => 'text', 'name' => 'Contacts[1][name]', 'id' => 'contacts-1-name',
'class' => 'form-error', 'maxlength' => 255 'class' => 'form-error', 'maxlength' => 255
), ),
array('div' => array('class' => 'error-message')), array('div' => array('class' => 'error-message')),
'The provided value is invalid', 'This is wrong',
'/div', '/div',
'/div' '/div'
); );
Expand Down

0 comments on commit 0119f79

Please sign in to comment.