Zend Framework 中的表单级验证

发布于 2024-08-20 02:06:46 字数 879 浏览 5 评论 0原文

我正在使用 Zend MVC 框架以及由 Propel 生成的 ORM 层,并且我试图找出从 Propel 对象的 save() 函数捕获异常的最佳方法,并将它们作为错误抛出到 Zend Form。

并非所有来自 Propel 对象的异常都有办法识别哪个字段导致了错误,所以我想知道是否有办法将通用错误消息添加到整个表单,而不是被迫附加每个字段特定表单元素的错误消息。

例如,我有一个包含在 try/catch 块中的 save() 调用,并且我可以将异常 -> getMessage() 添加到表单元素的错误中:

try {
    $obj->save();   
    echo 'object saved successfully';
} catch (Exception $e) {
    $form->name->addErrorMessage($e->getCode()." - ".$e->getMessage());
    $form->name->markAsError();
    $form->populate($formData);
}

但我希望能够执行以下操作

try {
    $obj->save();   
    echo 'object saved successfully';
} catch (Exception $e) {
    $form->addErrorMessage($e->getCode()." - ".$e->getMessage());
    $form->markAsError();
    $form->populate($formData);
}

:希望这是有道理的,谢谢你的帮助,

戴夫

I am using the Zend MVC framework along with an ORM layer generated with Propel, and I'm trying to figure out the best way to catch exceptions from a Propel object's save() function, and throw them to the Zend Form as errors.

Not all of the exceptions that come out of the Propel object have a way to identify which field caused the error, so I'm wondering if there is a way to add generic error messages to the entire form, rather than being forced to attach each error message to a particular form element.

For example, I have a save() call wrapped in a try/catch block, and I can add the exception->getMessage() to a form element's errors:

try {
    $obj->save();   
    echo 'object saved successfully';
} catch (Exception $e) {
    $form->name->addErrorMessage($e->getCode()." - ".$e->getMessage());
    $form->name->markAsError();
    $form->populate($formData);
}

but I would like to be able to do something like this:

try {
    $obj->save();   
    echo 'object saved successfully';
} catch (Exception $e) {
    $form->addErrorMessage($e->getCode()." - ".$e->getMessage());
    $form->markAsError();
    $form->populate($formData);
}

I hope that makes sense, thanks for the help,

Dave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

柠檬色的秋千 2024-08-27 02:06:46

您是否正在考虑来自数据库或 Propel 验证层(尚未开发那么多,并且默认情况下在 save() 步骤中不使用)的错误?

如果您想使用数据库错误,请记住它们只会返回第一个错误(因此如果用户输入了三个错误,则必须提交四次)。此外,从错误消息中获取字段名称可能很困难。请记住,某些键涵盖多个字段(“namefirst_name 的组合必须是唯一的”)。

这就是 Symfony 在表单层添加验证的原因。在那里,您可以一次检查所有字段,并返回多个错误。但也许您已经这样做了,并且只想将其作为最终检查?

Are you thinking about the errors from the database, or from the Propel validation layer (which isn't developed that much, and not used by default in the save() step)?

If you want to use the database errors, keep in mind that they will only return the first error (so the user has to submit four times if they entered three errors). Also, getting the field name out of the error message can be hard. Keep in mind that some keys cover multiple fields ("the combination of name and first_name must be unique").

This is why for example Symfony adds validation in the form layer. There, you can check all fields at once, and return multiple errors. But maybe you already do this, and only want this as a final check?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文