php 如何使用 Laravel 中的旧输入重定向?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30002608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:28:51  来源:igfitidea点击:

How can I redirect with old input in Laravel?

phpformslaravellaravel-5blade

提问by cyber8200

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).
  • 错误信息
  • 和旧输入。

enter image description here

在此处输入图片说明



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 ?

有人可以纠正我吗?我忘记做什么了吗?在 Laravel 中完成这样的事情最有效的方法是什么?

回答by toesslab

You can access the last inputs like so:

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

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

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

As pointed at by @Arian Acostayou may simply do

正如@Arian Acosta所指出的,你可以简单地做

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

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

As described in the docsit is more convenient in a blade view.

文档中所述,在刀片视图中更方便。



There are several possibilties in the controller:

控制器中有几种可能性:

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

代替 ->withInput(Request::except('password'))

do:

做:

a)Input::flash();

一种)Input::flash();

or:

或者:

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

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

and redirect to the view with:

并重定向到视图:

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

一种)->withInput(Input::except('password'));

resp. with:

分别 和:

b)->withInput();

b)->withInput();

The docs about Old Inputfor further reading...

用于进一步阅读的有关旧输入文档...

回答by Collin James

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

您缺少输入元素上的值...

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

回答by Petro

For my project, I needed another way:

对于我的项目,我需要另一种方式:

Shove a GETrequest in the validation return to fetch some previous data:

GET在验证返回中推送请求以获取一些先前的数据:

   // if the validator fails, redirect back to the form
    if ($validator->fails()) {
        //let's send some data back (Orlando,Florida):
        return Redirect::to('auth/register?city=Orlando&State=Florida')
            ->withErrors($validator)


    }else{
     //validation success
    }