找到你要的答案

Q:How can I redirect with old input in Laravel? Controller Form

Q:我怎么能把在Laravel的老输入? 控制器 形式

I have a login modal. When the user log-in fail the authentication, I want to redirect back to this modal with :

  • error message(s)
  • and old input(s).


Controller

// if Auth Fail 
return Redirect::to('/')
                ->with('error','Username/Password Wrong')
                ->withInput(Request::except('password'))
                ->withErrors($validator);

Form

{!! Form::open(array('url' => '/', 'class' => 'login-form')) !!}

  <div class="form-group">
    <label for="username">Username</label>
    <input type="text" class="form-control"  id="username" name="username" placeholder="Enter Username" required>
  </div>
  <div class="form-group">
    <label for="password">Password</label>
    <input type="password" class="form-control" id="password" name="password" placeholder="Enter Password" required>
  </div>
  <button type="submit" class="btn btn-primary">Login</button>

{!! Form::close() !!}

As you can see as part of my image, the error message seem to display, but the old input of username doesn't seem to populate.

Can someone please correct me ? Did I forget to do anything ? What is the most efficient way in Laravel to accomplish something like this ?

我有一个登录模式。当用户登录失败的身份验证,我想重新回到这个模式与:

  • error message(s)
  • and old input(s).


控制器

// if Auth Fail 
return Redirect::to('/')
                ->with('error','Username/Password Wrong')
                ->withInput(Request::except('password'))
                ->withErrors($validator);

形式

{!! 形式::open(array('url' => '/', 'class' => 'login-form')) !!}

  <div class="form-group">
    <label for="username">Username</label>
    <input type="text" class="form-control"  id="username" name="username" placeholder="Enter Username" required>
  </div>
  <div class="form-group">
    <label for="password">Password</label>
    <input type="password" class="form-control" id="password" name="password" placeholder="Enter Password" required>
  </div>
  <button type="submit" class="btn btn-primary">Login</button>

{!! 形式::close() !!}

正如你可以看到的一部分,我的形象,错误信息似乎显示,但旧的输入用户名似乎不填充。

Can someone please correct me ? Did I forget to do anything ? What is the most efficient way in Laravel to accomplish something like this ?

answer1: 回答1:

You can access the last inputs like so:

$username = Request::old('username');

As pointed at by @Arian Acosta you may simply do

<input type="text" ... value="{{ old('username') }}" ... >.

As described in the docs it is more convienient in a blade view.


There are several possibilties in the controller:

Instead of ->withInput(Request::except('password'))

do:

a) Input::flash();

or:

b) Input::flashExcept('password');

and redirect to the view with:

a) ->withInput(Input::except('password'));

resp. with:

b) ->withInput();

The docs about Old Input for further reading...

您可以访问像这样的最后输入:

用户名为:美元请求:老('username”);

正如在“Arian Acosta你可以简单地做

输入类型=“文本”…价值=“{ {老('username ')} }”…& gt;。

描述文档更方便在叶片角度。


有几种可能性的控制器:

Instead of ->withInput(Request::except('password'))

做:

a)输入::flash();

或:

B)输入::flashexcept('password”);

并重定向到视图与:

一)- >;内输入(输入::除了('password '));

分别为。随着:

B)- >;withinput();

文件关于进一步阅读旧的输入…

answer2: 回答2:

You are missing the value on your input element...

value="{{ Input::get ('username', '') }}"

您缺少输入元素的值…

value="{{ Input::get ('username', '') }}"
php  forms  laravel  laravel-5  blade