手动应用形式错误 [英] Applying form errors manually

查看:89
本文介绍了手动应用形式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我在一个更大的上下文中编辑一段数据。用户将此数据提交到用于处理的特定动作,并重定向回到父页面。因为它是一个重定向,验证错误没有得到自动设置,所以我想解决这个问题。

I have a situation where I'm editing a snippet of data within a larger context. The user submits this data to a specialized action for handling and redirects back to the parent page. Because it's a redirection, validation errors aren't getting automagically set, so I'm trying to work around that.

如果发生错误,我写 $ model-> validationErrors 的会话的 validation_errors 键。在形式上,虽然,我想告诉Cake设置每个错误,所以我可以利用我现有的风格,而不必对我的 $ this-> Form-> input()方法。

In the event of an error, I'm writing a validation_errors key to the session with a value of $model->validationErrors. In the form, though, I'd like to tell Cake to set each error so I can leverage my existing styles and not have to make a lot of changes to my $this->Form->input() methods.

这样的东西可能吗?基本上,我正在寻找手动实现相同的结果,你会得到如果一个常规表单提交并允许通过验证错误。我希望我可以循环遍历每个验证错误,并设置字段错误,但这不是任何改变。

Is something like this possible? Essentially, I'm looking to manually achieve the same result you'd get if a regular form was submitted and allowed to drop through with validation errors. I was hoping I could loop over each validation error and set the field error, but that's not making any change at all.

谢谢。

推荐答案

这可以在控制器中通过

$this->Model->invalidate('fieldName', __('ErrorMessage', true));

如果值可用,您还可以调用

If the values are available, you can also call

$this->Model->validates();

以验证模型中定义的验证器的所有值。

to validate all values with the validators defined in the model.

这篇关于手动应用形式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆