Browse Source

Fixing input generation for collections when prefixing table name

Jose Lorenzo Rodriguez 12 years ago
parent
commit
0119f79adf

+ 5 - 0
src/View/Form/EntityContext.php

@@ -217,6 +217,11 @@ class EntityContext implements ContextInterface {
 		}
 
 		$lastProp = $this->_rootName;
+
+		if ($path[0] === $this->_rootName) {
+			$path = array_slice($path, 1);
+		}
+
 		foreach ($path as $prop) {
 			$next = $this->_getProp($entity, $prop);
 			if (

+ 28 - 0
tests/TestCase/View/Form/EntityContextTest.php

@@ -231,6 +231,34 @@ class EntityContextTest extends TestCase {
 	}
 
 /**
+ * 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.
  *
  * @dataProvider collectionProvider

+ 13 - 16
tests/TestCase/View/Helper/FormHelperTest.php

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