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.
You can do that check manually I guess
<input type="password" name="password" @if ($errors->has('password')) autofocus @endif >
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 >
@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();
}
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 >
@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.
If you pass an array you don't have any issues ;)
It doesn't really makes sense to pass an array for variables like autofocus? Can you focus more than one field at a time ?
@bobbybouwmann
code results in
Undefined variable: autofocus (View: C:\xampp\htdocs\projekt\resources\views\auth\login.blade.php)
@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']);
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 >
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);
@premsaurav
Could you please post me code for controller and view?
All code I tried until now from this thread doesn't work :(.
@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 sign in or create an account to participate in this conversation.