Skip to content

Commit b9da325

Browse files
committed
Add tests and fix implementatino for nested array fields.
1 parent 9932a3f commit b9da325

3 files changed

Lines changed: 51 additions & 5 deletions

File tree

src/View/Form/DocumentContext.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,20 @@ public function error(string $field): array
417417
$entityErrors = $this->_context['entity']->getErrors();
418418
}
419419

420+
$tailField = array_pop($parts);
420421
if ($entity instanceof Document) {
421-
$errors = $entity->getError(array_pop($parts));
422+
$errors = $entity->getError($tailField);
422423
}
423424

424-
if (!$errors && $entityErrors) {
425+
// If errors couldn't be read from $entity and $entity
426+
// is either not an array, or the tail field is not Document
427+
// we want to extract errors from the root entity as we could
428+
// have nested validators, or structured fields.
429+
if (
430+
!$errors &&
431+
$entityErrors &&
432+
(!is_array($entity) || !($entity[$tailField] instanceof Document))
433+
) {
425434
$errors = Hash::extract($entityErrors, $field) ?: [];
426435
}
427436

tests/TestCase/View/Form/DocumentContextTest.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,10 +473,10 @@ public function testNestedValidatorErrors()
473473
$expected = [ 'username' => [ 'Required' ] ];
474474
$this->assertEquals($expected, $context->error('user'));
475475

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

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

526+
/**
527+
* Test errors for structured fields.
528+
*
529+
* @return void
530+
*/
531+
public function testErrorNestedFields()
532+
{
533+
$row = new Article([
534+
'name' => [
535+
'ja' => [
536+
['family_name' => 'XX', 'given_name' => 'YY'],
537+
],
538+
'en' => [
539+
['family_name' => 'ZZ', 'given_name' => 'AA'],
540+
]
541+
]
542+
]);
543+
$row->setError('name', [
544+
'ja' => [
545+
['family_name' => ['_empty' => 'Invalid value']]
546+
]
547+
]);
548+
549+
$articles = $this->setupIndex();
550+
$context = new DocumentContext(
551+
$this->request,
552+
[
553+
'entity' => $row,
554+
'table' => $articles,
555+
'validator' => 'default',
556+
]
557+
);
558+
$this->assertEquals(['_empty' => 'Invalid value'], $context->error('name.ja.0.family_name'));
559+
$this->assertEquals([], $context->error('name.derp.0.family_name'));
560+
$this->assertEquals([], $context->error('name.derp.0.undefined'));
561+
}
562+
526563
/**
527564
* Test getting fieldnames.
528565
*

tests/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
putenv('DB_URL=Cake\ElasticSearch\Datasource\Connection://127.0.0.1:9200?driver=Cake\ElasticSearch\Datasource\Connection');
5252
}
5353

54-
ConnectionManager::setConfig('elastic', ['url' => getenv('DB_URL')]);
5554
ConnectionManager::setConfig('test', ['url' => getenv('DB_URL')]);
55+
ConnectionManager::setConfig('test_elastic', ['url' => getenv('DB_URL')]);
5656

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

0 commit comments

Comments
 (0)