Browse Source

Implemented ContextInterface::isPrimary, this helps deciding whether
a field is primary in a multi-record form or when dealing with
associations

Jose Lorenzo Rodriguez 12 years ago
parent
commit
487c4aac7d

+ 8 - 0
src/View/Form/ArrayContext.php

@@ -111,6 +111,14 @@ class ArrayContext implements ContextInterface {
 	}
 
 /**
+ * {@inheritDoc}
+ */
+	public function isPrimaryKey($field) {
+		$primaryKey = $this->primaryKey();
+		return in_array($field, $primaryKey);
+	}
+
+/**
  * Returns whether or not this form is for a create operation.
  *
  * For this method to return true, both the primary key constraint

+ 9 - 0
src/View/Form/ContextInterface.php

@@ -27,6 +27,15 @@ interface ContextInterface {
 	public function primaryKey();
 
 /**
+ * Returns true if the passed field name is part of the primary key for this context
+ *
+ * @param string $field A dot separated path to the field a value
+ *   is needed for.
+ * @return boolean
+ */
+	public function isPrimaryKey($field);
+
+/**
  * Returns whether or not this form is for a create operation.
  *
  * @return boolean

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

@@ -154,6 +154,23 @@ class EntityContext implements ContextInterface {
 	}
 
 /**
+ * {@inheritDoc}
+ */
+	public function isPrimaryKey($field) {
+		$parts = explode('.', $field);
+
+		if ($parts['0'] === $this->_rootName) {
+			$parts = array_slice($parts, 1);
+		}
+
+		list(, $prop) = $this->_getEntity($parts);
+		$table = $this->_getTable($prop);
+
+		$primaryKey = (array)$table->primaryKey();
+		return in_array(array_pop($parts), $primaryKey);
+	}
+
+/**
  * Check whether or not this form is a create or update.
  *
  * If the context is for a single entity, the entity's isNew() method will

+ 7 - 0
src/View/Form/NullContext.php

@@ -52,6 +52,13 @@ class NullContext implements ContextInterface {
 /**
  * {@inheritDoc}
  */
+	public function isPrimaryKey() {
+		return false;
+	}
+
+/**
+ * {@inheritDoc}
+ */
 	public function isCreate() {
 		return true;
 	}

+ 1 - 2
src/View/Helper/FormHelper.php

@@ -931,9 +931,8 @@ class FormHelper extends Helper {
  */
 	protected function _inputType($fieldName, $options) {
 		$context = $this->_getContext();
-		$primaryKey = (array)$context->primaryKey();
 
-		if (in_array($fieldName, $primaryKey)) {
+		if ($context->isPrimaryKey($fieldName)) {
 			return 'hidden';
 		}
 

+ 5 - 30
tests/TestCase/View/Helper/FormHelperTest.php

@@ -5509,7 +5509,6 @@ class FormHelperTest extends TestCase {
  * @return void
  */
 	public function testFormMagicInputLabel() {
-		$this->markTestIncomplete('Need to revisit once models work again.');
 		TableRegistry::get('Contacts', [
 			'className' => __NAMESPACE__ . '\ContactsTable'
 		]);
@@ -5573,43 +5572,19 @@ class FormHelperTest extends TestCase {
 
 		$result = $this->Form->input('1.id');
 		$this->assertTags($result, array('input' => array(
-			'type' => 'hidden', 'name' => 'Contact[1][id]',
-			'id' => 'Contact1Id'
+			'type' => 'hidden', 'name' => '1[id]',
+			'id' => '1-id'
 		)));
 
 		$result = $this->Form->input("1.name");
 		$expected = array(
-			'div' => array('class' => 'input text'),
-			'label' => array('for' => 'Contact1Name'),
+			'label' => array('for' => '1-name'),
 			'Name',
 			'/label',
 			'input' => array(
-				'type' => 'text', 'name' => 'Contact[1][name]',
-				'id' => 'Contact1Name', 'maxlength' => '255'
-			),
-			'/div'
-		);
-		$this->assertTags($result, $expected);
-
-		$result = $this->Form->input('Contact.1.id');
-		$this->assertTags($result, array(
-			'input' => array(
-				'type' => 'hidden', 'name' => 'Contact[1][id]',
-				'id' => 'Contact1Id'
+				'type' => 'text', 'name' => '1[name]',
+				'id' => '1-name', 'maxlength' => '255'
 			)
-		));
-
-		$result = $this->Form->input("Model.1.name");
-		$expected = array(
-			'div' => array('class' => 'input text'),
-			'label' => array('for' => 'Model1Name'),
-			'Name',
-			'/label',
-			'input' => array(
-				'type' => 'text', 'name' => 'Model[1][name]',
-				'id' => 'Model1Name'
-			),
-			'/div'
 		);
 		$this->assertTags($result, $expected);
 	}