在 Laravel 中编辑表单时显示旧值的最佳做法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38461677/
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
What is the best practice to show old value when editing a form in Laravel?
提问by naneri
I am writing the logic for an edit form and have some complications when displaying data in the inputs.
我正在编写编辑表单的逻辑,并且在输入中显示数据时有一些复杂性。
When I initially show the form, I show the records values like:
当我最初显示表单时,我显示的记录值如下:
value="{{$dog->title}}"
Then when the form does not pass the validation I need to show the old input, so that user does not loose what he has already input. So I need to have a way to display old data like:
然后当表单未通过验证时,我需要显示旧输入,以便用户不会丢失他已经输入的内容。所以我需要有一种方法来显示旧数据,例如:
value="{{old('title')}}"
Because I need to input old data in case it exists, I ended up with this code:
因为我需要输入旧数据以防它存在,所以我最终得到了以下代码:
value="{{$dog->title or old('title')}}"
And in controller I check if Request has old input, I assign the $dog var a null value.
在控制器中,我检查 Request 是否有旧输入,我为 $dog var 分配一个空值。
I wanted to ask if that is considered an OK practice or is there a better and 'correct' way to do it?
我想问一下这是否被认为是一种好的做法,或者是否有更好和“正确”的方法来做到这一点?
回答by ikurcubic
Function oldhave defaultparameter if no old data found in session.
如果在会话中未找到旧数据,则函数old具有默认参数。
function old($key = null, $default = null)
You can replace expression in template with
您可以将模板中的表达式替换为
value="{{old('title', $dog->title)}}"
回答by Jelly Bean
I know this has already been answered but I thought I would leave a little snippet here for others in the future.
我知道这已经得到了回答,但我想我将来会在这里为其他人留下一些片段。
Setting old value on input as @ikurcubic posted can be used the same way on radio button or select:
在输入上设置旧值作为@ikurcubic 发布可以在单选按钮或选择上以相同的方式使用:
<input type="text" name="name" value="{{ old('name', $DB->default-value) }}" />
Select option:
选择选项:
<option value="Jeff" {{ old('name', $DB->default-value) == 'Jeff' ? 'selected' : '' }}>Jeff</option>
Radio Button:
单选按钮:
<input type="radio" name="gender" value="M" {{ old('name', $DB->default-value)== "M" ? 'checked' : '' }} />
Another way of doing it; write a small if statement to determine which value should be evaluated:
另一种方法;写一个小的 if 语句来确定应该评估哪个值:
@php
if(old('name') !== null){
$option = old('name');
}
else{ $option = $database->value; }
@endphp
<select name="name">
<option value="Bob" {{ $option == 'Bob' ? 'selected' : '' }}>Bob</option>
<option value="Jeff" {{ $option == 'Jeff' ? 'selected' : '' }}>Jeff</option>
</select>
<input type="radio" name="gender" value="M" {{ $option == "M" ? 'checked' : '' }} />
<input type="radio" name="gender" value="F" {{ $option == "F" ? 'checked' : '' }} />
Setting old value of input with an array name e.g name="name[]":
使用数组名称设置输入的旧值,例如 name="name[]":
<input type="text" name="name[]" value="{{ old('name.0) }}" />
This will give you the old value of the input with an index of 0.
这将为您提供索引为 0 的输入的旧值。
I have tested this and it works.
我已经测试过这个并且它有效。
回答by Adeel Ahmed Baloch
I solved that issue in my application if you want to get old previous inserted value in input you should try this one I did like this.
我在我的应用程序中解决了这个问题,如果你想在输入中获得以前插入的旧值,你应该试试这个我喜欢的。
function SaveData(Request $request)
{
$sValidationRules = [
'firstname' => 'required',
'lastname' => 'required',
'email' => 'required',
'phone' => 'required',
];
$validator = Validator::make($request->all(), $sValidationRules);
if ($validator->fails()) // on validator found any error
{
// pass validator object in withErrors method & also withInput it should be null by default
return redirect('Your Route Hare')->withErrors($validator)->withInput();
}
Employee::create($request->all());
return redirect(' your route hare ');
}
& after then get old data in input field value like this using {{ Request::old('firstname') }} Happy Coding. :)
& 然后使用 {{ Request::old('firstname') }} Happy Coding 在输入字段值中获取旧数据。:)
<input type="text" name="firstname" id="firstname" value="{{ Request::old('firstname') }}" class="form-control" placeholder="First Name">
回答by Fernando Borba
Another way to do that is get the data from the dog
class, like this:
另一种方法是从dog
类中获取数据,如下所示:
value="{{old('title') ?? $dog->title }}"
Why? Because old()
is for validation; when you fail validation, the input will remain available in the field. In the case where validation hasn't fired yet, value
will be filled with $dog->title
.
为什么?因为old()
是为了验证;当您验证失败时,输入将在该字段中保持可用。在验证尚未触发的情况下, value
将填充$dog->title
.
回答by Rob Fonseca
There is nothing wrong with the way you are doing things as Laravel gives multiple ways to handle the situation you're describing.
你做事的方式没有错,因为 Laravel 提供了多种方法来处理你所描述的情况。
What I would suggest is using the Laravel Collective Form and HTML packages to build your form. This package will automatically handle binding old request values to your form if validation fails
我建议使用 Laravel Collective Form 和 HTML 包来构建表单。如果验证失败,此包将自动处理将旧请求值绑定到您的表单