1

I am using Services 3.x for creating web services for mobile application, and I am using the JSON format.

The validation errors given from the user register services comes with HTML tags; for example the email already exists error is the following one.

<em> the@email.com</em> is already in use.  Have you forgotten your password?".

How do I strip those HTML tags from the error message?

2

hook_services_request_postprocess_alter() looks like a good candidate. I don't have an installation to test with right now but something like this ought to work:

function MYMODULE_services_request_postprocess_alter($controller, $args, &$result) {
  if (isset($result['form_errors'])) {
    array_walk($result['form_errors'], function(&$error) {
      $error = strip_tags($error);
    });
  }
}
  • MYMODULE_services_request_postprocess does not work but MYMODULE_services_request_postprocess_alter works for some of the endpoints. It worked for the default services in the module but not for custom services, I have created a custom services for user register login to save some extra data. – Puneet May 3 '17 at 6:11
  • Oops, yeah it has _alter at the end you're right. I don't know about the custom services, in theory there shouldn't be any difference as the results all go through the same processing regardless of source module – Clive May 3 '17 at 6:42
1

In Drupal 8 you can do it by using Markup:

use \Drupal\Core\StringTranslation\TranslatableMarkup;
...    
$message = new TranslatableMarkup('<b>@message!</b>', array('@message' => 'A bold error'));
$form_state->setError($message); 

OR

$mainMessage = "<em> the@email.com</em> is already in use.  Have you forgotten your password?";
$rendered_message = \Drupal\Core\Render\Markup::create($mainMessage);
\Drupal::messenger()->addMessage($rendered_message);

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.