Marcin Nabiałek
Marcin Nabiałek

Reputation: 111889

Laravel redirection using withInput doesn't allow to change inputs values

This is project from scratch, so if you want you can do exactly as I showed.

I created the following migration just to create users table and add new record:

public function up()
{
    Schema::create('users', function ($table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('name', 60)->unique();
        $table->string('email', 120)->unique();
        $table->string('password', 256);
        $table->rememberToken();
        $table->timestamps();
    });

    DB::table('users')->insert(
        [
            [
                'name'     => 'admin',
                'email'    => 'admin@admin',
                'password' => Hash::make('password')
            ]
        ]
    );
}

public function down()
{
    Schema::drop('users');
}

My routes are:

<?php

Route::get('/', [
    'as' => 'main_route',
    function () {
        return View::make('hello');
    }
]);

Route::post('/', [
    'as' => 'main_route',
    function () {
        if (!Auth::attempt(
            [
                'email'    => Input::get('email_to_fill'),
                'password' => Input::get('password_to_fill'),
            ]
        )
        ) {


        }

        return Redirect::route('main_route')->withInput();
    }
]);


Route::get('/logout', [
    'as' => 'logout',
    function () {
        Auth::logout();
        return Redirect::route('main_route');
    }
]);

My hello.blade.php is:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
@if (Auth::check())
    I'm logged in. E-mail {{ Auth::user()->email  }}

    <a href="{{ URL::route('logout') }}">Log out</a>

    {{Form::open(['url' => URL::route('main_route'), 'role' => 'form']) }}

    {{ Form::text('email', Auth::user()->email) }}


    {{Form::submit()}}
    {{Form::close() }}
@else
{{Form::open(['url' => URL::route('main_route'), 'role' => 'form']) }}

{{ Form::text('email', 'do not touch this') }}

{{ Form::text('email_to_fill', 'admin@admin') }}
{{ Form::text('password_to_fill', 'password') }}

{{Form::submit()}}
{{Form::close() }}
@endif
</body>
</html>

Nothing complicated so far.

So I open in my browser main page and click to send form (data is already filled in HTML so I don't need to fill in anything).

Below images:

Page before sending form

Page after login

After sending form I'm being logged in but as you see value of input is not correct. In code there is it should display in input value Auth::user()->email but it displays old value from email field before sending.

Question: Is it correct Laravel behaviour - when using withInput with redirect it will automatically fills all form inputs with the same values and even passing manual other value it will use the old data? Probably this example could be a bit simpler (without user login at all) but this is the exact problem I faced so I put it here as simple as I could.

Upvotes: 0

Views: 1036

Answers (1)

Joe
Joe

Reputation: 15812

This is the expected behaviour. I would post a more in-depth answer, but I can't think of any; that's just what it does :P

An example of why you might want this is if you're editing a record; it should load the record from the DB and populate the edit-form. Then you submit, and it turns out something failed validation. It should remember your posted data and prioritise it above the DB value so that you can change it.

The easiest fix would to to just not redirect ->withInput() if the authentication was successful.

Upvotes: 1

Related Questions