Oli Stockman
Oli Stockman

Reputation: 322

Extend Validate Class behaviour in CakePHP

I've been working on a Cake App all day and have had to copy two validation functions to nearly all of my models, which seems like an awful waste.

I've had a look around on both the Cake documentation and here without much fruit, if I want to extend the Validate class so that I only have to code these validate functions, what's the best way of going about it, and where would the suitable file fit best in the CakePHP structure?

I'm guessing I'll need to do something along the lines of:

class extraValidation extends Validate {

  public function alphaNumericSpecialValues($check) {

    //validation routines

  }

}

Any help greatly appreciated

Upvotes: 0

Views: 532

Answers (1)

ori
ori

Reputation: 7847

It's not easy to override Validation, since its method are static and they're called in a static way (using the class name).

However, you can achieve what you want by putting your custom validation functions either in AppModel (which is the superclass of your models), or in a behavior. If you put them in a behavior remember to add &$Model as the first argument of each function.

Upvotes: 1

Related Questions