duange051858 2010-02-01 22:10
浏览 61
已采纳

Zend Framework中的表单级别验证

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

  • 写回答

1条回答 默认 最新

  • dongtui8593 2010-02-02 06:54
    关注

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c