Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

t0berius's avatar

return back with autofocus on form element

In case of a user entered a incorrect password I would like them to be redirected back to the form and the autofocus should be set to the password "form".

if($userdetails == null)
    {
        return back()->withErrors(trans('account.username'))->withInput();
    }

Where to set now

'autofocus'=>'password'

? password is the name of the input field itself.

0 likes
15 replies
bobbybouwmann's avatar

You can do that check manually I guess

<input type="password" name="password" @if ($errors->has('password')) autofocus @endif>
1 like
d3xt3r's avatar

If you are looking for how to pass it to your view

 return back()->withErrors(trans('account.username'))->withInput()->with('autofocus','password');

then

<input type="password" name="password" @if (isset($autofocus) && $autofocus === 'password') autofocus @endif>
t0berius's avatar

@bobbybouwmann

But how to do this into my example code? As you may see in this example there isn't a validator for checking. How to get the "name" of the error so I can use your code for checking?

if($userdetails == null)
{
    return back()->withErrors(trans('account.username'))->withInput();
}
bobbybouwmann's avatar

Yeah you can simply pass the data you need to the view, you can do it even easier then @premsaurav provided

 return back()->withErrors(trans('account.username'))->withInput()->with('autofocus', ['password']);


<input type="password" name="password" @if (in_array('password', $autofocus)) autofocus @endif>

For failed you can check if the session has an array and add a class for example

<input type="password" name="password" class=" @if ($errors->has('password')) error-class @endif" @if (in_array('password', $autofocus)) autofocus @endif>
d3xt3r's avatar

@bobbybouwmann

in_array('password', $autofocus)

can lead to Undefined variable: autofocus in normal cases, the reason i used the isset() check. If he always will be setting this variable, what you said makes sense.

d3xt3r's avatar

It doesn't really makes sense to pass an array for variables like autofocus? Can you focus more than one field at a time ?

t0berius's avatar

@bobbybouwmann

code results in

Undefined variable: autofocus (View: C:\xampp\htdocs\projekt\resources\views\auth\login.blade.php)

t0berius's avatar

@bobbybouwmann @premsaurav I've edited code so it doesn't produce errors but it still doesn't work. view:

 <div>
    Password
    <input type="password" name="password" @if (isset($autofocus)) autofocus @endif>        
</div>

controller:

return back()->withErrors(trans('account.username'))->withInput()->with('autofocus', ['password']);
d3xt3r's avatar

Try this, you are not checking for this variable in controller and assigning it back to view, so ...

<input type="password" name="password" @if (Session::has('autofocus'))) autofocus @endif> 
bobbybouwmann's avatar

Well like I said in be beginning, it's easier to check it in the view itself. However it should work fine.

You can also do this if you only have one field with autofocus

->with('autofocus', true);
t0berius's avatar

@premsaurav

Could you please post me code for controller and view? All code I tried until now from this thread doesn't work :(.

d3xt3r's avatar
d3xt3r
Best Answer
Level 29

@jaheller Lets keep it simple. below works for me

// Controller
 return back()->with('autofocus', true);

// view 
<input type="password" name="password" @if (Session::has('autofocus')) autofocus @endif>

Check the source of your page, so you see autofocus, if yes your browser is failing to recognize it.

Please or to participate in this conversation.