如何在 Laravel 中创建带有步骤的表单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15911395/
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
How to do a form with steps in Laravel?
提问by Prash
I'm trying to create a "Step-by-step" form with Laravel. The trouble I'm having is how to let the user go "back" and edit the previously submitted part of the form.
我正在尝试使用 Laravel 创建一个“分步”表单。我遇到的麻烦是如何让用户“返回”并编辑以前提交的表单部分。
I'm doing the regular validation stuff, here's an example of the form field:
我正在做常规的验证工作,这是表单字段的示例:
{{ Form::text('title', Input::old('title') }}
{{ Form::text('title', Input::old('title') }}
Then, if the form for that particular step passes validation, this will be saved in a session as an array and the user will be taken to the next part of the form.
然后,如果该特定步骤的表单通过验证,这将作为数组保存在会话中,用户将被带到表单的下一部分。
Want I want to have is a "back" button on each step where they can go back to the previous step an change it. The problem is that I have Input::old()
for the value parameter of the input.
想要我想要的是每一步都有一个“后退”按钮,在那里他们可以回到上一步进行更改。问题是我有Input::old()
输入的值参数。
One thing I could do is assign a variable like "current" and set that to the session for that particular step, so my syntax for the inputs would look like this:
我可以做的一件事是分配一个像“当前”这样的变量并将其设置为该特定步骤的会话,因此我的输入语法如下所示:
{{ Form::text('title', (!empty($current)) ? $current['title'] : Input::old('title')) }}
{{ Form::text('title', (!empty($current)) ? $current['title'] : Input::old('title')) }}
Is there a better way of doing this?
有没有更好的方法来做到这一点?
回答by Phill Sparks
ShawnMcCool's Form Base Model provides a good mechanism for both handling input values without the need for ugly ternaries, and also support for multipage forms. I'll not list any code here, instead recommend that you read through the bundle's examples:
ShawnMcCool 的 Form Base Model 提供了一种很好的机制来处理输入值而不需要丑陋的三元组,并且还支持多页表单。我不会在这里列出任何代码,而是建议您阅读捆绑包的示例:
https://github.com/ShawnMcCool/laravel-form-base-model#examples
https://github.com/ShawnMcCool/laravel-form-base-model#examples
回答by mattl
I might have a multi dimensional array with the input from each page stored under page number.
我可能有一个多维数组,其中每个页面的输入都存储在页码下。
$saved = Session::get('inputtedstuff');
$current = $saved['page1'];
Then whichever page you are on you can grab that pages previous answers if there are any. And at the end you can get all the answers together and map them to a flat array for further use.
然后,无论您在哪个页面上,都可以抓取该页面以前的答案(如果有)。最后,您可以将所有答案放在一起并将它们映射到平面数组以供进一步使用。
I would also, depending on the complexity of the form, consider doing the form all in one page and use javascript to show each part. Just a thought.
我也会,根据表单的复杂程度,考虑在一页中完成表单并使用 javascript 来显示每个部分。只是一个想法。