(Laravel)表单验证不返回旧输入

时间:2016-12-16 23:16:49

标签: php forms laravel validation laravel-5

我使用的是Laravel 5.3。验证失败时,我有一个表单没有用旧输入填充字段。表格如下:

{{ Form::open(['route' => 'my.route', 'class' => 'form-horizontal', 'files' => true]) }}

<div class="form-group">
    {{ Form::label('name', 'Name', ['class' => 'control-label col-md-3']) }}
    <div class="col-md-9">
        {{ Form::text('name', null, ['placeholder' => 'hello world' ,'class' => 'form-control']) }}
    </div>
</div>

<div class="form-group">
    {{ Form::label('description', 'Description', ['class' => 'control-label col-md-3']) }}
    <div class="col-md-9">
        {{ Form::textarea('description', null, ['placeholder' => 'hello world', 'class' => 'form-control']) }}
        <span>some sub-text</span>
    </div>
</div>

<div class="form-group">
    {{ Form::label('date', 'Date', ['class' => 'control-label col-md-3']) }}
    <div class="col-md-9">
        {{ Form::text('date', null, ['placeholder' => 'hello world', 'class' => 'form-control']) }}
    </div>
</div>

{{ Form::close() }}

我正在处理这样的信息:

    public function store(ItemRequest $request, ImageMagick $imageMagick)
    {
        $item = new Item;
        $item->name = $request->name;
        $item->description = $request->description;
        $item->date = $request->date;

        $item->save();
        return redirect()->route('some.other.route');
    }

我正在我自己的表单请求文件中进行验证&#34; ItemRequest&#34;看起来像这样:

class ItemRequest extends FormRequest
{
    public static $rules = [
        'name' => 'required|min:5|max:255',
        'description' => 'required|min:50',
        'date' => 'required'
    ];

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        if ($this->isMethod('post')) {
            return $this->createRules();
        }

        return $this->updateRules();
    }

    public function createRules()
    {
        return self::$rules;
    }

    public function updateRules()
    {
        return array_merge(
            self::$rules,
            $this->otherRules()
        );
    }

    public function otherRules()
    {
       return ['age' => "required"];
    }
}

我认为问题在于ItemRequest。我尝试在store方法中添加 - &gt; withInput()到我的返回但旧的输入仍未保留。

  return redirect()->route('some.other.route')->withInput();

但它没有改变。

编辑:当页面刷新时我正在清除旧输入。

1 个答案:

答案 0 :(得分:0)

您必须在输入字段区域中加入value="{{ Input::old('Code')}}"。因此,在描述文本区域中,您可以使用

<div class="form-group">
    {{ Form::label('description', 'Description', ['class' => 'control-label col-md-3']) }}
    <div class="col-md-9">
        {{ Form::textarea('description', null, ['placeholder' => 'hello world', 'class' => 'form-control','value'=>'Input::old('description')']) }}
        <span>some sub-text</span>
    </div>
</div>