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

Error Messages Not Displayed for Nested Array Fields in Form Inputs #323

Open
ryu818 opened this issue Nov 26, 2024 · 2 comments
Open

Error Messages Not Displayed for Nested Array Fields in Form Inputs #323

ryu818 opened this issue Nov 26, 2024 · 2 comments
Labels

Comments

@ryu818
Copy link
Contributor

ryu818 commented Nov 26, 2024

Summary

When validating multidimensional array data (e.g., name.ja.0.family_name), validation errors occur as expected, but the corresponding error messages are not displayed in the form input fields.

Steps to Reproduce

  1. Define validation for fields with the following data structure:
    "name": {
          "ja": [
            {
              "family_name": "XX",
              "given_name": "XX"
            }
          ],
          "en": [
            {
              "family_name": "XX",
              "given_name": "XXX"
            }
          ]
    }
  2. Define validation as follows:
    $langs = ['ja', 'en'];
    $requiredNames = ['family_name', 'given_name'];
    $langValidator = new AppValidator();
    foreach ($langs as $lang) {
        $nameValidator = new AppValidator();
        foreach ($requiredNames as $name) {
            $nameValidator
                ->scalar($name)
                ->maxTextboxLength($name)
                ->requirePresence($name, 'create')
                ->notEmptyString($name);
        }
        $langValidator
            ->array($lang)
            ->allowEmptyArray($lang)
            ->addNestedMany($lang, $nameValidator);
    }
    $validator->addNested('name', $langValidator)
        ->array('name')
        ->notEmptyArray('name');
  3. Define form controls as follows:
    echo $this->Form->control(
        'name.ja.0.family_name',
        [
            'type' => 'text',
            'required' => true,
            'label' => [
                'text' => __d('cv', 'Family name') . __d('cv', '(Japanese)'),
                'required' => false,
            ],
        ],
    );
  4. Submit the form with family_name left empty.

Observed Behavior

Validation errors are triggered, and the following error is generated:

'name' => [
    'ja' => [
        (int) 0 => [
            'family_name' => [
                '_empty' => 'The provided value must be scalar',
            ],
        ],
    ],
],

However, the error message does not appear in the corresponding input field in the form.

Cause

The issue lies in the following section of the DocumentContext class:
https://github.com/cakephp/elastic-search/blob/4.x/src/View/Form/DocumentContext.php#L424

Current code:

if (!$errors && $entityErrors && !is_array($entity)) {

This condition skips error processing when $entity is an array, resulting in the error message not being propagated to the form field.

Suggested Fix

Modify the condition as follows to ensure errors are processed correctly for arrays:

if (!$errors && $entityErrors) {

Expected Behavior After Fix

Even when $entity is a multidimensional array, errors should be processed correctly, and the error messages should appear in the corresponding form input fields.

Question

Is this fix appropriate, or is the issue caused by a problem in the validation method itself?

@markstory markstory added the bug label Nov 26, 2024
@markstory
Copy link
Member

Is this fix appropriate, or is the issue caused by a problem in the validation method itself?

I think your suggested fix makes sense. Would you be interested in opening a pull request with a fix?

@ryu818
Copy link
Contributor Author

ryu818 commented Nov 27, 2024

I have submitted a pull request at #324. Please review it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants