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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?