Laravel 4 - 显示带有 OLD 数据输入和 DB 信息的编辑表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23762841/
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
Laravel 4 - Showing edit form with OLD data input as well as DB information
提问by BigJobbies
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 }}"
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?
是否有允许我放入数据库数据的旧位参数?
Cheers,
干杯,
回答by Igor R.
Hey you could validate and return ->withInput() and then in your actual form, check if there is Input::old() and display it, otherwise display from the db.
嘿,您可以验证并返回 ->withInput() 然后在您的实际表单中,检查是否有 Input::old() 并显示它,否则从数据库显示。
example:
例子:
<input type="text" name="subject"
value="{{ (Input::old('subject')) ? Input::old('subject') : $letter->subject }}">
Or you could go the other way and define the variable and do a regular if statement, instead of the ternary one! Up to you to decide what you want to use!
或者你可以走另一条路并定义变量并执行常规 if 语句,而不是三元语句!由您决定要使用什么!
回答by Jarek Tkaczyk
All you need is form model binding http://laravel.com/docs/html#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() }}
This way you form will be populated by the $letter data (passed from the controller for example).
这样你的表单将被 $letter 数据填充(例如从控制器传递)。
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.
然后在重定向时,您的表单将首先重新填充输入值,而不是原始模型数据。
回答by Nadeem0035
Make it more simple and clean
让它更简单干净
<input type="text" name="subject" value="{{ (Input::old('subject')) ?: $letter->subject }}">
回答by ikurcubic
I'm not sure for Laravel 4 but in Laravel 5, function oldtakes second param, default value if no old data in session.
我不确定 Laravel 4,但在 Laravel 5 中,如果会话中没有旧数据,函数old需要第二个参数,默认值。
Please check this answer Best practice to show old value
请检查此答案最佳实践以显示旧值