Skip to content
Permalink
Browse files

Fix required state detection for booleans on associations.

The original value of $field was being replaced with the short name,
this caused type() to get the wrong type data for association columns.

Refs #7349
  • Loading branch information...
markstory committed Sep 13, 2015
1 parent a2a381c commit ffecea9de34f35bf56e0e0eca12032163d583e7b
Showing with 31 additions and 3 deletions.
  1. +3 −3 src/View/Form/EntityContext.php
  2. +28 −0 tests/TestCase/View/Form/EntityContextTest.php
@@ -348,12 +348,12 @@ public function isRequired($field)
}
$validator = $this->_getValidator($parts);
$field = array_pop($parts);
if (!$validator->hasField($field)) {
$fieldName = array_pop($parts);
if (!$validator->hasField($fieldName)) {
return false;
}
if ($this->type($field) !== 'boolean') {
return $validator->isEmptyAllowed($field, $isNew) === false;
return $validator->isEmptyAllowed($fieldName, $isNew) === false;
}
return false;
}
@@ -739,6 +739,34 @@ public function testIsRequiredAssociatedHasMany()
$this->assertFalse($context->isRequired(''));
}
/**
* Test isRequired on associated entities with boolean fields
*
* @return void
*/
public function testIsRequiredAssociatedHasManyBoolean()
{
$this->_setupTables();
$comments = TableRegistry::get('Comments');
$comments->schema()->addColumn('starred', 'boolean');
$comments->validator()->add('starred', 'valid', ['rule' => 'boolean']);
$row = new Article([
'title' => 'My title',
'comments' => [
new Entity(['comment' => 'First comment']),
]
]);
$context = new EntityContext($this->request, [
'entity' => $row,
'table' => 'Articles',
'validator' => 'default',
]);
$this->assertFalse($context->isRequired('comments.0.starred'));
}
/**
* Test isRequired on associated entities with custom validators.
*

0 comments on commit ffecea9

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.