I added an address field on a content type and a required integer field. When switching countries the address widget stays the same and I get this error:

Recoverable fatal error: Argument 1 passed to Drupal\Core\Form\FormState::setError() must be of the type array, null given, called in /core/lib/Drupal/Core/Field/WidgetBase.php on line 448 and defined in Drupal\Core\Form\FormState->setError() (line 1157 of /core/lib/Drupal/Core/Form/FormState.php).

I found a temporary workaround for my case and disabled validation until the form is submitted:

public function validateForm(array &$form, FormStateInterface $form_state) {
  if($form_state->getTriggeringElement()['#type'] == 'submit') {
    parent::validateForm($form, $form_state);
  }
}
CommentFileSizeAuthor
#3 having_a_required-2602004-3.patch660 bytessubhojit777

Comments

Lukas von Blarer created an issue. See original summary.

subhojit777’s picture

Project: Address » Drupal core
Version: 8.x-1.x-dev » 8.0.x-dev
Component: Code » field system

I tested this with "required" text (plain) field, and it worked without any error. Looks like this is the problem of number widget.

subhojit777’s picture

Status: Active » Needs review
StatusFileSize
new660 bytes
amateescu’s picture

Status: Needs review » Postponed (maintainer needs more info)

Just tried to reproduce this with latest code for core and the address module and switching countries when a required number widget is also present on the node form works just fine. Can you please test again on a clean Drupal 8 install or provide any other steps that are needed to reproduce the issue?

In any case, the patch is not correct because \Drupal\Core\Field\Plugin\Field\FieldWidget\NumberWidget::formElement() returns its form array for the 'value' key, so errorElement() needs to do the same.

amateescu’s picture

Project: Drupal core » Address
Version: 8.0.x-dev » 8.x-1.x-dev
Component: field system » Code
Status: Postponed (maintainer needs more info) » Closed (duplicate)

Actually, it seems this is a duplicate of #2619878: Recoverable fatal error: Argument 1 passed to Drupal\\Core\\Form\\ FormState::setError() must be of the type array, which also happens only with the Address module, so moving back to its queue.

subhojit777’s picture

  1. Git clone latest Drupal code git clone --branch 8.0.x http://git.drupal.org/project/drupal.git
  2. Git clone latest composer_manager module code git clone --branch 8.x-1.x http://git.drupal.org/project/composer_manager.git
  3. Execute php modules/composer_manager/scripts/init.php, composer drupal-update (as per https://github.com/commerceguys/commerce#installation)
  4. Git clone latest address module code git clone --branch 8.x-1.x http://git.drupal.org/project/address.git
  5. Enable address module
  6. Create new content type
  7. Add address field (default settings)
  8. Add number integer field (make it required)
  9. Go to add content page of that content type
  10. Select any country

Expected behavior:
Address fields appear based on the selected country, and no PHP warnings are logged.
Current behavior:
The address fields will not appear, and you will see PHP warnings and error in recent log message.

Now:

  1. Change the number field and make it unrequired
  2. Go to add content page of that content type
  3. Select any country

Observation:
Address fields appear based on the selected country, and no PHP warnings are logged.

subhojit777’s picture

More observation:

  1. Remove the number field that was added in #6
  2. Add another field say, plain text field and make it required
  3. Go to content add page
  4. Select any country

Observation:
Address fields are added based on the selected country, and no PHP error messages are logged.

This made me think that there is issue with Number field widget.