Skip to content

Commit

Permalink
Make boolean fields not required by default.
Browse files Browse the repository at this point in the history
Having boolean fields (checkboxes) marked as required by default makes
working with optional checkboxes really hard. Instead we won't
automatically mark checkboxes as required.

Refs #5158
  • Loading branch information
markstory committed Nov 20, 2014
1 parent 2b54b12 commit 6bcc414
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/View/Form/EntityContext.php
Expand Up @@ -331,8 +331,10 @@ public function isRequired($field) {
if (!$validator->hasField($field)) {
return false;
}
$allowed = $validator->isEmptyAllowed($field, $isNew);
return $allowed === false;
if ($this->type($field) !== 'boolean') {
return $validator->isEmptyAllowed($field, $isNew) === false;
}
return false;
}

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/TestCase/View/Form/EntityContextTest.php
Expand Up @@ -592,6 +592,32 @@ public function testValAssociatedCustomIds() {
$this->assertEquals([1, 4], $result);
}

/**
* Test validator for boolean fields.
*
* @return void
*/
public function testIsRequiredBooleanField() {
$this->_setupTables();

$context = new EntityContext($this->request, [
'entity' => new Entity(),
'table' => 'Articles',
]);
$articles = TableRegistry::get('Articles');
$articles->schema()->addColumn('comments_on', [
'type' => 'boolean'
]);

$validator = $articles->validator();
$validator->add('comments_on', 'is_bool', [
'rule' => 'boolean'
]);
$articles->validator('default', $validator);

$this->assertFalse($context->isRequired('title'));
}

/**
* Test validator as a string.
*
Expand Down

0 comments on commit 6bcc414

Please sign in to comment.