Error when we have custom form with ajax

Created on 19 February 2024, 4 months ago
Updated 20 February 2024, 4 months ago

Problem/Motivation

the problem when we have a form with a field address, we use the form hook and we need to ajax another set of fields in the same form, but we only want to modify the subform, not all forms with an address of field. then the field widget always sends the field address to the validation form, the ajax form definitely doesn't check the validation on the address it will call errorElement. The element in this case is null
TypeError: Drupal\address\Plugin\Field\FieldWidget\AddressDefaultWidget::errorElement(): Argument #1 ($element) must be of type array, null given, called in C:\Users\www\drupal\web\core\lib\Drupal\Core\Field\WidgetBase.php on line 580 in Drupal\address\Plugin\Field\FieldWidget\AddressDefaultWidget->errorElement() (line 188 of C:\Users\www\drupal\web\modules\contrib\address\src\Plugin\Field\FieldWidget\AddressDefaultWidget.php).
Or this error

	TypeError: Drupal\Component\Utility\NestedArray::getValue(): Argument #1 ($array) must be of type array, null given, called in C:\Users\www\drupal\web\modules\contrib\address\src\Plugin\Field\FieldWidget\AddressDefaultWidget.php on line 190 in Drupal\Component\Utility\NestedArray::getValue() (line 69 of C:\Users\www\drupal\web\core\lib\Drupal\Component\Utility\NestedArray.php).

Proposed resolution

This patch will avoid this error. it should check the value before use, avoid undefined key in variable

πŸ› Bug report
Status

Needs review

Version

2.0

Component

Code

Created by

πŸ‡«πŸ‡·France lazzyvn paris

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @lazzyvn
  • πŸ‡«πŸ‡·France lazzyvn paris
  • Status changed to Needs review 4 months ago
  • πŸ‡·πŸ‡ΈSerbia bojanz

    @lazzyvn
    We need either a test or a small form class where the problem can be observed.

    Changing status to "needs review" to signal that there is a patch here.

  • πŸ‡«πŸ‡·France lazzyvn paris

    I tested twice on 2 machines and it drives me crazy. Same version, same project but one has an error, the other does not. it never calls the formError.
    Environment has an error Drupal 10.2.3, PHP 8.2

Production build 0.69.0 2024