Skip to content
Permalink
Browse files

Implemented ContextInterface::isPrimary, this helps deciding whether

a field is primary in a multi-record form or when dealing with
associations
  • Loading branch information...
lorenzo committed Feb 28, 2014
1 parent bc65994 commit 487c4aac7df6ef82b2f91db7ab5573d24b4550ac
@@ -110,6 +110,14 @@ public function primaryKey() {
return [];
}
/**
* {@inheritDoc}
*/
public function isPrimaryKey($field) {
$primaryKey = $this->primaryKey();
return in_array($field, $primaryKey);
}
/**
* Returns whether or not this form is for a create operation.
*
@@ -26,6 +26,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.
*
@@ -153,6 +153,23 @@ public function primaryKey() {
return (array)$this->_tables[$this->_rootName]->primaryKey();
}
/**
* {@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.
*
@@ -49,6 +49,13 @@ public function primaryKey() {
return [];
}
/**
* {@inheritDoc}
*/
public function isPrimaryKey() {
return false;
}
/**
* {@inheritDoc}
*/
@@ -931,9 +931,8 @@ protected function _parseOptions($fieldName, $options) {
*/
protected function _inputType($fieldName, $options) {
$context = $this->_getContext();
$primaryKey = (array)$context->primaryKey();
if (in_array($fieldName, $primaryKey)) {
if ($context->isPrimaryKey($fieldName)) {
return 'hidden';
}
@@ -5509,7 +5509,6 @@ public function testForMagicInputNonExistingNorValidated() {
* @return void
*/
public function testFormMagicInputLabel() {
$this->markTestIncomplete('Need to revisit once models work again.');
TableRegistry::get('Contacts', [
'className' => __NAMESPACE__ . '\ContactsTable'
]);
@@ -5573,43 +5572,19 @@ public function testFormMagicInputLabel() {
$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);
}

0 comments on commit 487c4aa

Please sign in to comment.
You can’t perform that action at this time.