I have a form which, upon failed validation, should query a custom/non-Drupal table, and if that query is successful, to redirect the user to a custom page that is only accessible through a logged-in state (so I need to "fake" successful authentication even if the user does not exist in the official Drupal users table).

The query to the custom table essentially checks a secondary user list in non-Drupal format upon failing to authenticate the official way, and if successful, would redirect the user to another page where they could select a Drupal username and password combination in order to register them in the system / make an official account.

This is the relevant code inside my form validation method:

if ($this->userAuth->authenticate($form_state->getValue('name'), $password) == FALSE) {
    // other stuff
    $uid = $this->userAuth->authenticate($form_state->getValue('name'), $password);
    $form_state->set('uid', $uid);

    $response = new TrustedRedirectResponse('/path/destination');

I have also tried this instead of the new TrustedRedirectResponse:

    $redirect_path = "/path/destination";
    $url = url::fromUserInput($redirect_path);

closed as unclear what you're asking by 4k4, Pierre.Vriens, DRUPWAY, Neograph734, kiamlaluno Apr 23 '18 at 15:48

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • You didn't ask a question, but generally if you redirect to an internal path you use the route name $form_state->setRedirect('my.redirect.route'); – 4k4 Apr 7 '18 at 8:25
  • If there is a destination set, you need to reset it first, see drupal.stackexchange.com/questions/250465/… – 4k4 Apr 7 '18 at 8:30

If you want the redirect to happen immediately, you can do it like this:

use Symfony\Component\HttpFoundation\RedirectResponse;

$response = new RedirectResponse('/path/destination');

  • Sending and returning seems a bit like overkill. In a form validation or submit method you can't do either of it, put the response in $form_state instead and the form submitter will take care of returning the response at the right time. – 4k4 Apr 7 '18 at 8:18
  • Yeah, the return was unnecessary. I have edited and removed it. As for putting the response in the form state - it wouldn't kick in until form submission, while the poster indicated they wanted it immediately. – Jaypan Apr 9 '18 at 5:30
  • A StackPHP app like Drupal receives a request and returns a response. Sending the response breaks the interface REQ -> HttpKernel -> REP. – 4k4 Apr 9 '18 at 6:42
  • So how would you go about doing the redirect on a failed validation? – Jaypan Apr 9 '18 at 13:33
  • Validation and submitting are executed back to back, you should be able to put the redirect response in both. Only difference is, if you set a validation error in $form_state (which is not the case here), then submitting is stopped and the error is displayed in the form itself (which wouldn't make sense in case of a redirect). – 4k4 Apr 9 '18 at 14:09

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