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?

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

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏