Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Error Messages Not Displayed for Nested Array Fields in Form Inputs #324

Merged
merged 2 commits into from
Dec 7, 2024
Merged
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
13 changes: 11 additions & 2 deletions src/View/Form/DocumentContext.php
Original file line number Diff line number Diff line change
@@ -417,11 +417,20 @@ public function error(string $field): array
$entityErrors = $this->_context['entity']->getErrors();
}

$tailField = array_pop($parts);
if ($entity instanceof Document) {
$errors = $entity->getError(array_pop($parts));
$errors = $entity->getError($tailField);
}

if (!$errors && $entityErrors && !is_array($entity)) {
// If errors couldn't be read from $entity and $entity
// is either not an array, or the tail field is not Document
// we want to extract errors from the root entity as we could
// have nested validators, or structured fields.
if (
!$errors &&
$entityErrors &&
(!is_array($entity) || !($entity[$tailField] instanceof Document))
) {
$errors = Hash::extract($entityErrors, $field) ?: [];
}

41 changes: 39 additions & 2 deletions tests/TestCase/View/Form/DocumentContextTest.php
Original file line number Diff line number Diff line change
@@ -473,10 +473,10 @@ public function testNestedValidatorErrors()
$expected = [ 'username' => [ 'Required' ] ];
$this->assertEquals($expected, $context->error('user'));

$expected = [ 'Required' ];
$expected = ['Required'];
$this->assertEquals($expected, $context->error('user.username'));

$expected = [ 'Required' ];
$expected = ['Required'];
$this->assertEquals([], $context->error('comments.0'));
$this->assertEquals($expected, $context->error('comments.0.comment'));
$this->assertEquals($expected, $context->error('comments.2.comment'));
@@ -523,6 +523,43 @@ public function testErrorAssociatedHasMany()
$this->assertEquals([], $context->error('comments.1.article_id'));
}

/**
* Test errors for structured fields.
*
* @return void
*/
public function testErrorNestedFields()
{
$row = new Article([
'name' => [
'ja' => [
['family_name' => 'XX', 'given_name' => 'YY'],
],
'en' => [
['family_name' => 'ZZ', 'given_name' => 'AA'],
]
]
]);
$row->setError('name', [
'ja' => [
['family_name' => ['_empty' => 'Invalid value']]
]
]);

$articles = $this->setupIndex();
$context = new DocumentContext(
$this->request,
[
'entity' => $row,
'table' => $articles,
'validator' => 'default',
]
);
$this->assertEquals(['_empty' => 'Invalid value'], $context->error('name.ja.0.family_name'));
$this->assertEquals([], $context->error('name.derp.0.family_name'));
$this->assertEquals([], $context->error('name.derp.0.undefined'));
}

/**
* Test getting fieldnames.
*
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -51,8 +51,8 @@
putenv('DB_URL=Cake\ElasticSearch\Datasource\Connection://127.0.0.1:9200?driver=Cake\ElasticSearch\Datasource\Connection');
}

ConnectionManager::setConfig('elastic', ['url' => getenv('DB_URL')]);
ConnectionManager::setConfig('test', ['url' => getenv('DB_URL')]);
ConnectionManager::setConfig('test_elastic', ['url' => getenv('DB_URL')]);

$schema = new MappingGenerator('./tests/mappings.php', 'test');
$schema->reload();
Loading