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

我怕爱的太早我们不能终老 提交于 2019-12-11 14:03:46

问题


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:

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.


回答1:


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.



来源:https://stackoverflow.com/questions/25933629/laravel-redirection-using-withinput-doesnt-allow-to-change-inputs-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!