在将帖子发送到请求的控制器函数之前进行 CakePHP 表单验证

发布于 2024-10-19 07:21:43 字数 650 浏览 1 评论 0原文

我的验证按原样工作,但我想在搜索控制器之前显示验证错误。我知道这在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

草莓酥 2024-10-26 07:21:43

您可以使用模型的 validates() 方法来检查它是否验证,然后重定向回来。

假设您的模型称为 Plan,这将是您的控制器

$errors = array();
if (!$this->Plan->validates($this->data)) {
   //errors occured
   $errors = $this->Plan->invalidFields();
   $this->Session->save('Plan.errors', $errors);
   $this->redirect('/plans');
}

并且在您看来。

if ($this->Session->check('Plan.errors')) {
   $errors = $this->Session->read('Plan.errors');
   $this->Session->delete('Plan.errors'); //don't want it again

}

在这两种情况下,请确保会话助手/组件实际上已分配给您的视图和控制器

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

$errors = array();
if (!$this->Plan->validates($this->data)) {
   //errors occured
   $errors = $this->Plan->invalidFields();
   $this->Session->save('Plan.errors', $errors);
   $this->redirect('/plans');
}

And in your view.

if ($this->Session->check('Plan.errors')) {
   $errors = $this->Session->read('Plan.errors');
   $this->Session->delete('Plan.errors'); //don't want it again

}

In both cases, make sure Session helper/component is actually assigned to your view and controller

云淡风轻 2024-10-26 07:21:43

好的。破解了一个内爆的工作解决方案,但现在我的错误仅显示在默认布局中,不再显示在所属的表单字段下。所以现在我需要知道如何将错误返回到表单字段下方。

工作代码:

...else {
          $errors = $this->Plan->invalidFields();
          $error_messages = implode(' ',$errors);
          $this->Session->setFlash($error_messages);
          $this->redirect('/');
        }...

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:

...else {
          $errors = $this->Plan->invalidFields();
          $error_messages = implode(' ',$errors);
          $this->Session->setFlash($error_messages);
          $this->redirect('/');
        }...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文