Sunday, May 25, 2014

callback succession / why afterValidate() if validation fails

Dear Bakers,

I have an app under CakePHP 2.5.1. I'm not sure to understand how the callback succession is supposed to work, eg if the validation fails, there is IMHO no reason to run afterValidate() ? But it does run indeed.

What I'm trying to do:

my form has 7 entry fields. The ones I have issues with are start_time, end_time and duration. The user might enter a combination of them, eg

- start & end
- start & duration
- duration & end
- only duration

(at the end, I only care about durations)

My strategy for saving the data is

- have a validation for the fields, eg to get the right time format (HH:MM) or duration, but with 'required' => false and 'allowEmpty' => true
- in afterValidate(), I verify that the new entered start and end do not overlap with existing records
- in beforeSave(), I verify the combination entered by the user, the goal being to calculate a duration

My problem is that even if the user enters a wrong format for a field, Cake still run afterValidate(), causing problems in my queries, before I can return "false" to abort the save.

Now I'm not really sure why cake runs afterValidate() if validation fails. Is there something I missed ?

TIA for your assistance,
Charles

No comments: