Skip to content

Commit

Permalink
Merge pull request #324 from ryu818/fix/issue323/nested_error_mes
Browse files Browse the repository at this point in the history
fix: Error Messages Not Displayed for Nested Array Fields in Form Inputs
  • Loading branch information
markstory authored Dec 7, 2024
2 parents dbc65ae + b9da325 commit f474c62
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/View/Form/DocumentContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) ?: [];
}

Expand Down
41 changes: 39 additions & 2 deletions tests/TestCase/View/Form/DocumentContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f474c62

Please sign in to comment.