Redirect to Different URl on validation errors in form builder I am using drupal 8 fapi and i have user case as below

I have a form  on https://example.com/landing-page and I submit the  form and there are validation errors .the page should reload as https://example.com/landing-page#lead-form.

Is there any hack we can implement?

I came with logic

if validation has error set form redirect page to example.com/landing-page#lead-form

if($form_state-&gt;getErrors() ) {
  $response = new TrustedRedirectResponse('example.com/landing-page#lead-form');
  $form_state-&gt;setResponse($response);
}


some how its not working.

Is there any way we can change the url to example.com/landing-page#lead-form and show the validation error at same time .
You can use like

For Internal:

global $base_url;
$response = new TrustedRedirectResponse($base_url.'/landing-page#lead-form');
// You can use any one of these below to show error message.
$form_state-&gt;setError($element, $message);
$form_state-&gt;setErrorByName($element, $message);
$form_state-&gt;setResponse($response);


For External:
Use full link

$response = new TrustedRedirectResponse('https://facebook.com');
// You can use any one of these below to show error message.
$form_state-&gt;setError($element, $message);
$form_state-&gt;setErrorByName($element, $message);
$form_state-&gt;setResponse($response);