Feb15th

CakePHP model validation

One thing I miss about Ruby on Rails is the model validation. Right out of the box CakePHP does not have great validation. I’ve been specifically looking for a good implementation of validating the uniqueness of an attribute.

After doing some research, Evan of evansagge.com has written a fantastic solution to CakePHP’s lack of model validation. I’ve implemented it and it works very nicely. Here are some of the builtin validation options:

  • validateNotEmpty
  • validatePattern (you can pass through any regex pattern you would like)
  • validateWord - for validating that there’s only regular characters
  • validateInteger
  • validateNumber
  • validateFloat
  • validateDouble
  • validateEmail
  • validateUnique
  • validateLength - specify a min and max
  • and more…

His Validation also gives very nice methods of accessing the error messages in your templates.