php - Laravel 5.6 - 表单在验证后不会重新填充以前的值

标签 php laravel laravel-5

我在 Laravel 5.6 中有一个简单的表单,我正在运行验证,我的表单看起来像这样..

   @if ($errors->any())
        <div class="alert alert-danger">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

<form action="{{route('posts.store')}}" method="POST">
   <input name="title" type="text">#
    <button type="submit" class="btn btn-success">Submit</button>
</form>

我的 Controller 是这样的..

public function store(Request $request)
    {

        $this->validate($request, [
            'title' => 'required',
        ]);

        $post = new Post;
        $title = $request->input('title');
        $post->save();
    }

这是可行的,但是当验证失败并显示错误消息时,它不会用以前的值重新填充表单。在 https://laravel.com/docs/5.2/requests#old-input 阅读文档它说我不需要做任何事情,它会自动进行。

有人知道我哪里出错了吗?

最佳答案

如果你想保留之前的输入,你需要使用old helper function ,如您向下滚动到“检索旧输入”时在链接文档中看到的那样。

您需要将您的输入更改为以下内容:

<input name="title" type="text" value="{{ old('title') }}">

title 与您输入的 name 属性中的值匹配。

关于php - Laravel 5.6 - 表单在验证后不会重新填充以前的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51122025/

相关文章:

php - 持护照的多用户 - 如何获取经过身份验证的用户类型

php - 在 Digital Ocean Ubuntu 上设置 Laravel 5.1

php - 拉拉维尔 5.2 : session and token guard on the same routes

javascript - 返回数组而不是对象 laravel/javascript ajax 调用

javascript - AJAX、Javascript 显示脚本运行进度

php - 将 SQL SUM() 查询设置为 PHP 变量,并回显结果

php - 使用 2D 阵列在 Blade 中打印表格

php - Eloquent 模型中 id 周围的引号在生产站点上是必需的,但在本地主机上不起作用

javascript - 根据for循环的输出更改div颜色

PHP MYSQL 事件调度程序,如 Musicteacherhelper.com