CakePHP 1.2: Getting started with the new validation

January 1st, 2007  |  Published in Uncategorized  |  3 Comments

Getting Started with form validation

Nate replied to a question about using the new validation functions in CakePHP 1.2 dev versions, note: this is subject to change, I doubt that I will update this entry later.

Some validation rules

  • ‘rule’ => ‘alphaNumeric’
  • ‘rule’ => array(’between’, $min, $max)
  • ‘rule’ => array(’blank’)
  • ‘rule’ => array(’cc’, $type, $deep)
    • Where $type can be ‘fast’ (a basic check that covers all card types) or ‘all’ (check all cards) or an array of one or more of the following: ‘amex’,'bankcard’, ‘diners’, disc’, ‘electron’, ‘enroute’, ‘jcb’, ‘maestro’, ‘mc’, ’solo’, ’switch’, ‘visa’ or ‘voyager’.
  • ‘rule’ => array(’comparison’, $operator, $compare)
    • $operator may be one of the following: ‘isgreater’, ‘isless’, ‘greaterorequal’, ‘lessorequal’, ‘equalto’, ‘notequal’, or, their symbol equivalents: ‘>’, ‘<’, ‘>=’, ‘<=’, ‘==’, ‘!=’.
  • ‘rule’ => array(’custom’, $regex)
    • Where $regex is a custom regular expression.
  • ‘rule’ => array(’date’, $format)
    • Where $format is one of the following: ‘dmy’, ‘mdy’, ‘ymd’, ‘dMy’, ‘Mdy’, ‘My’, ‘my’. $format can also be an array containing mulitple values.
  • ‘rule’ => array(’decimal’, $precision)
    • Checks that the input is a decimal number with $precision places after the decimal point.
  • ‘rule’ => array(’email’, $deep)
    • If $deep is true, the email address will be checked for a valid host name.
  • ‘rule’ => array(’minLength’, $min)
  • ‘rule’ => array(’maxLength’, $max)
  • ‘rule’ => array(’numeric’)
  • ‘rule’ => array(’postal’)
  • ‘rule’ => array(’ssn’)
  • ‘rule’ => array(’url’)

More info at CakeBaker

Responses

  1. R13ckJ says:

    January 31st, 2007 at 4:55 am (#)

    Thanks for explaining this. I’m still having problems though. Any chance you could public a quick MVC tutorial for form validation (i.e code required in all three objects)? Would greatly appreciate it :)

  2. Gbush says:

    February 24th, 2007 at 2:02 am (#)

    Can you explain how we can add custom error messages to each rule or have multiple rules for one field ?

  3. Sam says:

    February 24th, 2007 at 8:14 am (#)

    Actually I haven’t started using 1.2 much so can’t help. http://bakery.cakephp.org/articles/view/55 might help with multiple validation. I would also try searching google for ‘cakephp advanced validation’ there are some good resources out these, but aimed at cake 1.1 not 1.2 yet.

Leave a Response