All About Validation in CakePHP 1.2 – Part 2

6 07 2007

In my previous article I discussed the new constructs for Model::validate. This article follows on from this and is a run down of the new validation methods and rules available.

First up there are a couple of regular expressions defined, which are the same from CakePHP 1.1 and are pretty self explanatory. These are VALID_NOT_EMPTY, VALID_NUMBER, VALID_EMAIL, and VALID_YEAR (between 1000-2999).

The general format of the following validation methods is

var $validate = array('field' => array('rule' => 'ruleName'));

Where parameters are required, such as between and cc, the format is

var $validate = array('field' => 
        array('rule' => array('ruleName', 'param1', 'param2'));

alphaNumeric
Allows only digits and a-z or A-Z.

between
Checks that a strings length is between a min and max value.

blank
Checks if a field is empty and treats whitespace characters as empty.

cc
Credit Card Number validation, includes luhn check and Card Type to number format. Takes one parameter which can be one of :-

  • fast – skips Card Type to number format check
  • all – checks the number against all card types until it finds a match
  • array of card types – like all but limited to a subset of cards.

e.g.

var $validate = array('field' => 
        array('rule' => array('cc', array('Visa')));

compare
Allows you to compare two numeric values. Takes two parameters:-

  • Operator – one of <, >, <=, >=, == or !=
  • Comparison value to compare against

custom
Allows you to use custom regular expressions. Takes the custom regex as the only parameter.

date
Validates a string as a date. Can take one parameter;

  • Format – default is ‘ymd’. other options are:
    • dmy
    • mdy
    • ymd
    • dMy – short or long month names
    • Mdy
    • My
    • my

decimal
Checks that a number has a decimal point or is scientific notation. Takes the number of decimal places required after the point as the only parameter. If places is null it will check for scientific notation.

email
Checks for a valid email address. If a parameter of true is passed it will also attempt to verify the host. If the parameter passed is false, or none is passed it behaves the same as VALID_EMAIL.

ip
Checks for IPv4 dot notation. e.g. 192.168.0.1

minLength
Checks a string for a minimum length. Length is passed as the only parameter.

maxLength
Checks a string for a maximum length. Length is passed as the only parameter.

money
Checks that a string is numbers, optionally grouped into blocks of 3 separated by a space, comma or period, with an optional block of 2 at the end. Can take a parameter of ‘right’ if you expect the currency symbol at the end, the default is at the start.

numeric
Simply calls is_numeric()

phone
Checks for a valid phone format. Takes regex and country as parameters. Currently only supports ‘us’ country option.

postal
Checks for a valid post code format. Takes regex and country as parameters. Currently only supports ‘us’, ‘uk’ and ‘ca’ country options.

ssn
Checks for a valid social security number format. Takes regex and country as parameters. Currently only supports ‘us’, ‘dk’ and ‘nl’ country options.

url
Checks for valid URL format. Supports http(s), ftp(s), file, news and gopher protocols

userDefined
Calls a userdefined method of the current model passing along any parameters. , the first of which is the method to call. Personally I think this is redundant as you can simply replace userDefined with your method name and it works the same.

There are also some incomplete methods listed below that will be coming soon.

number – checks that a number is within a given range
multiple – will be used for selects and multiple selects
equalTo – direct comparison to another value
file – checks for a file.

Advertisements

Actions

Information

12 responses

30 08 2007
CakePHP 1.2: The Romance Continues … « Ahsan’s Laboratory

[…] All about Validation in CakePHP 1.2 – Part 2 by Another Cake Baker […]

16 01 2008
allprocard

Thank you, very match !!!
Pay attention, this site http://allprocard.com
Very interesting information.

19 03 2008
MASHUP::Keyword

[…] link helped a […]

22 12 2008
Jaydeep Dave

‘url’ => array(
‘rule’ => ‘url’,
‘required’ => false,
‘allowEmpty’ => true,
‘message’ => ‘Invalid URL format.’
)

this validation accepts url like “test.com” ?

this is not a proper url format…….

18 02 2009
bbizbor

Я смотрю вас здесь уже заспамили

3 03 2009
CakePHP validation and validation contexts - tình huống validation trong cakePHP « Datgs’ Blog

[…] in CakePHP 1.2 there has been a major rework of the Validation class’s inner workings, and the […]

13 03 2009
kasienep

Tadalafil the prescription erectile dysfunction (ED) tablet clinically proven to both work fast (in as little as 30 minutes for some men) and work up to 36 hours vaiagra online generic ! For cialis buy uk fast delivery Europe. Save your money – buy cheap levitra pills on line.. Easy to get cheap cialis pills online… Find out more about levitra at the great viagera 50mg site..

27 03 2009
dawnsag

Levitra the prescription erectile dysfunction (ED) tablet clinically proven to both work fast (in as little as 30 minutes for some men) and work up to 36 hours cialias single dose . For levitra discount fast delivery Usa.. No prescription required -buy sexual health pill online.. Easy to get ed treatment pills online. Find out more about cheap viagra at the official cialas discount uk site!

27 03 2009
melanydic

Cheap levitra works in much the same way as Sildenafil Citrate, but is much faster, lasting up to 36 hours vaiagra cheap uk .. For levitra sildenafil citrate very fast delivery worldwide… Save your time – buy ed pills now… Fast to get tadalafil pills online. Find out more about cheap cialis at the online cialias order usa site.

2 05 2009
zeta

Validation in cakephp finally managed to work :).
I successfull used it, but I still have a “little” problem: I set the error message in the model (in the ‘validate’ array) but i wanto to include html tag. I insert a link in the message, but when it disply the error, I see the tags as I write them and not the link. Thank you

14 04 2010
ksu

cakephp gives a lot of to use

5 09 2016
Moldovans

is there a generic viagra available in the usa

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: