如果验证失败,如何恢复表单的值(在 Kohana 3 中)
官方文档底部有一个示例 http://kohanaframework.org/3.2/ guide/kohana/security/validation
但显然只要使用 View 中的 $post['username']
但 $post
,它就不会在请求中工作>第一次请求时数组为空。
那么在这种情况下如何恢复这些值呢?有什么通用的解决方案吗?
PS:是的,我知道我可以做 isset($post['username']) ? $post['username'] : '';
但这很烦人
There is a sample in the bottom of the official documentation http://kohanaframework.org/3.2/guide/kohana/security/validation
But obviously it wont work at the request as long as $post['username']
in View is used but the $post
array is empty on first request.
So how do you restore the values in this case? Any general solution?
PS: yes, I do understand I could do isset($post['username']) ? $post['username'] : '';
but it is just annoying
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我使用模型来显示表单中的数据。这样,初始表单值就是模型中的初始值。
然后,我使用控制器中的 POST 数据更新模型数据,如果存在验证错误,模型数据将包含 POST 数据。这意味着我不必在视图中放置任何条件逻辑,我只需这样做:
Form::input('name', $model->name)
下面是更详细的解释这种方法: Kohana ORM 和验证,遇到问题
I use the model to display the data in the form. That way the initial form value is the initial value in the model.
I then update the model data with POST data in the controller, if there are validation errors, the model data will contain the POST data. This means I don't have to put any conditional logic in the view, and I just do:
Form::input('name', $model->name)
Here's a more detailed explanation of this approach: Kohana ORM and Validation, having problems
我使用 Arr::get 函数:
I use Arr::get function:
我只是在查看关于构建和验证表单的旧文档。
从示例代码中可以看出,首先需要初始化一个以表单字段名称为键的数组,并将值设置为空字符串。如果有错误,请填写每个元素的值。在视图中,您只需正常调用
Form::input()
即可,无需任何if
语句或某种类型的语句。我猜 Kohana 从一开始就是这样构建的。而且似乎没有改变。您可能只需要做同样的事情。
I was just looking at the old documentation on Building and Validating a Form.
You can see from the sample code that first you need to initialize an array with the form field names as the key and set the value to an empty string. And if there's an error, fill in the values of each element. In the views, you can simply call
Form::input()
normally without anyif
statement or some sort.I guess Kohana has already been built this way from the start. And it doesn't seem to change. You'll probably just need to do the same thing.