在将帖子发送到请求的控制器函数之前进行 CakePHP 表单验证
我的验证按原样工作,但我想在搜索控制器之前显示验证错误。我知道这在 CakePHP 框架内可能是不可能的。
我有一个模型plan.php。在 plans_controller.php 中,我有一个名为 search() 的函数。
我的表单按预期调用 search() (因为没有搜索模型):
echo $this->Form->create('Plan', array('action' => 'search'));
就目前情况而言,当我提交搜索时,会显示错误,并且 url 更改为 .../search,因此没有显示任何结果(“该搜索条件有 0 个结果”,但正确的验证错误显示在必需的表单字段下方。
我不希望显示 .../search url。我希望表单“停止” “并且只显示验证错误,而不更改搜索功能的 url。
我在元素内调用搜索表单,因为搜索表单显示在几个不同的页面上。
总而言之:搜索表单应该验证而不进行验证将 url 路径更改为搜索的控制器操作名称当然,验证是在 search() 和 plan.php 模型中完成的,所以我只是不知道如何解决这个问题,并想知道它是否可能。
My validation is working as it stands, but I want to display the validation error prior to the search controller. I understand this might not be possible within the CakePHP framework.
I have a model plan.php. And in the plans_controller.php, I have a function called search().
My form calls search() as expected (because there is no search model):
echo $this->Form->create('Plan', array('action' => 'search'));
As it stands, when I submit my search, the errors are displayed and the url changes to .../search, so no results are displayed ("There are 0 results for that search criteria", but the correct validation errors are displayed below required form fields.
I do not want the .../search url to be displayed. I want the form to "halt" and just display the validation errors w/out changing the url to the search function.
I am calling the search form within an element because the search form displays on several different pages.
To sum this up: The search form should validate w/out changing the url path to the controller action name of the search. Of course, the validation is done IN the search() and plan.php model, so I just don't know how to work around this and wondering if its even possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用模型的 validates() 方法来检查它是否验证,然后重定向回来。
假设您的模型称为 Plan,这将是您的控制器
并且在您看来。
在这两种情况下,请确保会话助手/组件实际上已分配给您的视图和控制器
You can use the
validates()
method of the model to check whether it validates and then redirect back.Assuming your model is called Plan, this would be your controller
And in your view.
In both cases, make sure Session helper/component is actually assigned to your view and controller
好的。破解了一个内爆的工作解决方案,但现在我的错误仅显示在默认布局中,不再显示在所属的表单字段下。所以现在我需要知道如何将错误返回到表单字段下方。
工作代码:
OK. Cracked out a working solution w/ implode, but now my errors are only displayed in the default layout and no longer under the form fields where that belong.. So now I need to know how to get the errors back below the form fields..
Working code: