Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Datasource/EntityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,10 @@ protected function _nestedErrors(string $field): array
{
// Only one path element, check for nested entity with error.
if (!str_contains($field, '.')) {
if (!$this->has($field)) {
return [];
}

$entity = $this->get($field);
if ($entity instanceof EntityInterface || is_iterable($entity)) {
return $this->_readError($entity);
Expand All @@ -1126,7 +1130,9 @@ protected function _nestedErrors(string $field): array
$len = count($path);
$val = null;
if ($entity instanceof EntityInterface) {
$val = $entity->get($part);
if ($entity->has($part)) {
$val = $entity->get($part);
}
} elseif (is_array($entity)) {
$val = $entity[$part] ?? false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/View/Form/EntityContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public function val(string $field, array $options = []): mixed
}
}

$val = $entity->get($part);
$val = $entity->has($part) ? $entity->get($part) : null;
if ($val !== null) {
return $val;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/TestCase/ORM/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,9 @@ public function testGetErrorAndSetError(): void
$entity->setError('foo', 'bar');
$this->assertEquals(['bar'], $entity->getError('foo'));

$entity->requireFieldPresence(true);
$this->assertEquals([], $entity->getError('non_existent'));

$expected = [
'foo' => ['bar'],
];
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase/View/Form/EntityContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ public function testValBasic(): void
$result = $context->val('body');
$this->assertEquals($row->body, $result);

$row->requireFieldPresence(true);
$result = $context->val('nope');
$this->assertNull($result);
}
Expand Down
16 changes: 15 additions & 1 deletion tests/TestCase/View/Helper/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,10 @@ public function testCreateWithSecurity(): void
{
$this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
$encoding = strtolower(Configure::read('App.encoding'));
$result = $this->Form->create($this->article, [

$article = new Article();
$article->requireFieldPresence(true);
$result = $this->Form->create($article, [
'url' => '/articles/publish',
]);
$expected = [
Expand Down Expand Up @@ -2815,6 +2818,17 @@ public function testControl(): void
$this->assertHtml($expected, $result);
}

public function testControlEntityWithRequirePresence(): void
{
$article = new Article();
$article->requireFieldPresence(true);
$this->Form->create($article);
$this->Form->control('title');

// We only need to check that Cake\Datasource\Exception\MissingPropertyException is not thrown
$this->expectNotToPerformAssertions();
}

/**
* testControlCustomization method
*
Expand Down