数据验证/CakePhp 的行为

发布于 2024-10-12 06:14:27 字数 311 浏览 3 评论 0原文

我对 CakePHP 很陌生,但我有一个问题来证明我的一般理解。我正在为一家飞行培训公司写一份申请,该公司必须能够处理飞行记录。如果有人进入航班,则需要执行许多任务来检查输入的数据是否有效。因此,这不仅仅是使用模型提供的方法检查记录(例如,检查输入的时间是否有效的字段),还需要做更多的事情,例如检查他是否在着陆之前起飞(按时间顺序)。甚至问题是否已经有同一时间范围内的飞行记录。

所以我想知道这些检查是否要在控制器中完成,或者是否最好为此模型创建特定行为。或者,很明显我缺乏理解,我是否只需要在模型中使用 beforeInsert 函数即可。我读了很多帖子并用谷歌搜索,但我还没有得出结论。

I am quite new with CakePHP, but I have a question to proof my general understanding. I am write an application for a flight training company which has to be capable to deal with flight records. If somebody enters a flight there are many task to be carried out to check if the data entered is valid. So it's not just checking the record with the methods the model provides (e.g check a field if the time entered is valid) it's some more to be done like to check if he took off before he landed (timewise). Even the question if there is already a flight record in the same timeframe.

So I am wondering if these check are to be done in the controller or if it would be better to create a specific behaviour for this model. Or, and here comes clear that I am lacking of understanding, do I just have to use a beforeInsert function in my model. I've read a lot of posts and googled around, but I am not coming to a conclusion.

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

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

发布评论

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

评论(1

静谧 2024-10-19 06:14:27

您可能应该在模型中创建一个返回 true 或 false 的方法。我认为 beforeInsert 方法不适合这种情况 - 您将收到数据库插入错误和验证错误。我想您想向用户显示验证错误?

function validateRecord($record) {

   $return = array("bool" => true, "message" => "");

   // validation step failed

   $return['bool'] = false;
   $return['message'] = "Validation failed because etc etc";

   return $return;
}

You should probably create a method in your model returning either true or false. I don't think the beforeInsert method is appropriate for this - you'll be getting database insertion errors together with validation errors. I guess you would like to show validation errors to the user?

function validateRecord($record) {

   $return = array("bool" => true, "message" => "");

   // validation step failed

   $return['bool'] = false;
   $return['message'] = "Validation failed because etc etc";

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