Laravel 4-显示带有旧数据输入和DB信息的编辑表单 [英] Laravel 4 - Showing edit form with OLD data input as well as DB information

查看:59
本文介绍了Laravel 4-显示带有旧数据输入和DB信息的编辑表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序创建一个编辑表单,我想知道是否有人可以告诉我如何将数据库中的数据获取到我的文本字段中.

Im making a edit form for my app and i was wondering if someone could tell me how to get the data from the database into my text field.

我可以根据用户单击找到需要编辑的记录,并且如果执行以下操作,我可以显示信息:

I can locate the record i need to edit based on the users click, and i can display the information if i do the following:

value="{{ $letter->subject }}"

但是,我遇到的问题是,当我通过验证运行它并且出现错误时,它返回的是数据库信息而不是OLD数据.

BUT, the problem im having is that when i run it through the validation and there is an error, it comes back with the database information instead of the OLD data.

所以我的问题是.有没有办法先提供数据库信息,然后在通过验证时验证用户编辑的信息?

So my questions is. Is there a way to serve up the database information first and then when it goes through the validatior, validate the information the user has edited?

当前用于验证文本字段,并在出现错误的情况下使用

Currently to validate the text field and bring the data back incase of error, im using

Input::old('subject')

是否有一个允许我输入数据库数据的旧参数?

Is there a parameter for that old bit that allows me to put in the DB data?

干杯

推荐答案

您所需要做的就是表单模型绑定 http: //laravel.com/docs/html#form-model-binding :

All you need is form model binding http://laravel.com/docs/html#form-model-binding:

{{ Form::model($letter, ['route' => ['letters.update', $letter->id], 'method' => 'put']) }}
   // your fields like:
   {{ Form::text('someName', null, ['class' => 'someHTMLclass' ...]) }}
   // no default values like Input::old or $letter->something!
{{ Form::close() }}

这样,您的表单将由$ letter数据填充(例如,从控制器传递过来的数据).

This way you form will be populated by the $letter data (passed from the controller for example).

现在,如果您使用的是计数辊:

Now, if you have on your countroller:

// in case of invalid data
return Redirect::back()->withInput();

然后在重定向中,将首先使用输入值(而不是原始模型数据)重新填充表单.

then on the redirect your form will be repopulated with input values first, not the original model data.

这篇关于Laravel 4-显示带有旧数据输入和DB信息的编辑表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆