Adding validation to widget settings form I'm creating a new field in Drupal 8. I've created a class for the widget, extending WidgetBase and implementing WidgetInterface, and added the functions defaultSettings(), settingsForm() and settingsSummary().

I want/need to validate the element I'm adding in settingsForm(), but I can't seem to figure out how to add validation to this. I tried adding a function settingsFormValidate(), but that doesn't seem to be called. I also tried adding #element_validate to the element I'm adding in settingsForm(), but that also does not seem to be called.

Note that I'm not looking to validate the widget (that is shown to the user), I'm looking to validate the element added in settingsForm() for the widget, that is on the Manage Form Display tab, when adding the field to Entities/content types.

Does anyone know how or whether it's even possible to add validation to the widget settings form?
Once again, I've found the solution to my issue. Although I said that I had tried #element_validate in my original post, I must have messed something up, because that turned out to be the solution. Here is the code:

class SomeWidget extends WidgetBase implements WidgetInterface
{
  public function settingsForm(array $form, FormStateInterface $form_state)
  {
    $element['my_field'] = [
      '#type' =&gt; 'textfield',
      '#title' =&gt; t('My Field'),
      '#default_value' =&gt; $this-&gt;getSetting('my_field'),
      '#required' =&gt; TRUE,
      // Add your validation here
      '#element_validate' =&gt; [
        // settingsFormMyFieldValidate() is arbitrary. Choose something descriptive
        [$this, 'settingsFormMyFieldValidate'],
      ],
    ];

    return $element;
  }

  public function settingsFormMyFieldValidate($element, FormStateInterface $form_state)
  {
    $submitted_value = $form_state-&gt;getValue($element['#parents']);
    if($submitted_value != 'something it should equal')
    {
      $form_state-&gt;setError($element, t('You submitted a bad value'));
    }
  }
}