开发者

Behaviour for datavalidation/CakePhp

开发者 https://www.devze.com 2023-02-05 10:15 出处:网络
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 so

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 flig开发者_Go百科ht 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.


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消