8

我现在很困惑,我正在从 Laracast 学习 Laravel,根据讲师的说法,验证失败后,表单不会重置用户输入的值。但是在测试验证时,当我提交表单时会重置所有内容。

另一个问题是当我尝试访问它时未定义的变量。 $errors

我的控制器

<?php

namespace App\Http\Controllers;


use App\Articles;
use App\Http\Requests\CreateArticle;
use Carbon\Carbon;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ArticlesController extends Controller
{



    public function create()
    {
        return view('articles.create');
    }


    public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required',
            'body'  => 'required'
        ]);
        Articles::create($request->all());
        return redirect('articles');
    }

}

我的观点

@extends('app')

@section('content')
    <h1>Create a new Articles</h1>

    <hr/>

    {!! Form::open(['url' => 'articles']) !!}

    <div class="form-group">
        {!! Form::label('title', 'Title: ') !!}
        {!! Form::text('title', null, ['class' => 'form-control']) !!}
    </div>


    <div class="form-group">
        {!! Form::label('body', 'Body') !!}
        {!! Form::textarea('body', null, ['class' => 'form-control']) !!}
    </div>


    <div class="form-group">
        {!! Form::label('published_at', 'Published On:') !!}
        {!! Form::input('text', 'published_at', date('Y-m-d'), ['class' => 'form-control']) !!}
    </div>


    <div class="form-group">
        {!! Form::submit('submit', ['class' => 'btn btn-primary']) !!}
    </div>


    @if(isset($errors))
    {{var_dump($errors)}}
    @endif
    {!! Form::close() !!}

@stop

他使用 v5.0,我使用 v5.2

4

4 回答 4

5

从您的控制器返回以下内容...

return redirect('articles')->withInput();

看看是否有帮助。如果你想喜欢这个,你可以排除某些字段..

return redirect('articles')->withInput($request->only('email'))
于 2015-12-23T20:18:00.780 回答
4

There are a couple of issues here.

The first one, the inputs not being saved after validation fails. I believe this is because you are passing null into the functions which are building the inputs when you should be passing in the value you wish it to default to. Try the following...

<div class="form-group">
    {!! Form::label('title', 'Title: ') !!}
    {!! Form::text('title', old('title'), ['class' => 'form-control']) !!}
</div>

This should then populate the input element with the old data which was previously entered. Just follow the same procedure for the rest of the form inputs.

The second issue with the $errors value not being set is actually due to a change with Laravel 5.2. Many people have been having the same exact issue so I'd just like to refer you to a previous answer: Laravel 5.2 $errors not appearing in Blade

于 2015-12-23T20:31:59.680 回答
4

如果您不使用

<div class="form-group">
{!! Form::label('title', 'Title: ') !!}
{!! Form::text('title', old('title'), ['class' => 'form-control']) !!}
</div>

但有简单的输入字段,如

<input id="Email" name="Email" type="email" class="uit-input" placeholder="your email address" value={{old('Email')}}>

用旧数据“预填充”该值。

于 2017-06-17T06:52:19.577 回答
2

最适合你的方式

<div class="form-group">
        <label for="title">Title</label>
        <input type="text" name="upload_title" class="form-control" value="{{ (old('title')) ?  old('title') : $data->title}}">
</div>
于 2019-08-02T06:34:45.507 回答