0

I am using the form validation in hook_form_alter() as follows.

$form['#validate'][] = '\Drupal\module_name\Controller\MyController::_field_p_servicename_validate';

In the controller I am defining it as follows.

public function _field_p_servicename_validate(&$form, FormStateInterface $form_state) {}

It throws the following exception.

Strict warning: call_user_func_array() expects parameter 1 to be a valid callback, non-static method Drupal\module_name\Controller\MyController::_field_p_servicename_validate() should not be called statically in Drupal\Core\Form\FormValidator->executeValidateHandlers() (line 88 of core/lib/Drupal/Core/Form/FormValidator.php)

What does it mean? How can I fix it?

5

There are contexts where Drupal allows you to use a notation like '\Drupal\module_name\Controller\MyController::_field_p_rpservicename_validate' to refer to a public function of an object. In the case of validation handlers, since Drupal is calling them with call_user_func_array(), with a notation like '\Drupal\module_name\Controller\MyController::_field_p_rpservicename_validate' you are referring to a static function of a class.

Change the definition of that method as follows, and your code will work.

public static function _field_p_rpservicename_validate(&$form, FormStateInterface $form_state) {}

Keep in mind that a static method cannot use $this.

  • 1
    If this is in the form object, you can also specific the callback as '::_field_p_rpservicename_validate', that's a special form callback feature, starting with :: means a method on the form object. – Berdir Aug 1 '16 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.