I think we can do quite a lot to automate validation code.  It seems
natural to me to store basic validation requirements right along side
the
data definition.  For instance, we could place a list of validation
requirements
in a comment at the end of field definition line in a mysql script
file like so:

CREATE TABLE `users` (
...
`username` VARCHAR( 40 ) NOT NULL ,  # VALID_UNIQUE & alphaNumeric &
minLength(6)
`password` VARCHAR( 40 ) NOT NULL ,  # alphaNumeric & minLength(8)
...

Then some script could read the file and generate the "var $validate"
code
for the model.  This is useful in at least a couple ways:

(1) There are a lot of implied validations in the sql field
definitions,
    e.g., maximum length, not null, datetime vs. int, etc.

    One could also look for keywords in field names for clues,
    e.g., *email*, *phone*, *url*, etc.

    We may as well add our other validations here and keep them in one
place that's
    easy to update.  This alleviates the need to remember to go modify
the model code
    when you decide to change a field definition.

(2) It's also handy to have the whole set of validations on one line.
If you decide to
    move a field to another table, you just cut and paste the one
line, and all of
    the validations come along for the ride.

As a proof of concept, I wrote a script which translates the example
users.sql
file below into the validation code shown further down.  Some
validation rules
come from the SQL keywords and the rest are picked up from the
comments.
I jump between cakephp 1.1 and 1.2 a bit, but you get the idea.

Anyone know whether something like this exists?

Thanks,
-Adam

Input  ==============================================================
>>: cat users.sql
DROP TABLE IF EXISTS `users`;

CREATE TABLE `users` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT , # VALID_UNIQUE
`username` VARCHAR( 40 ) NOT NULL ,      # VALID_UNIQUE & alphaNumeric
`password` VARCHAR( 40 ) NOT NULL ,      # minLength(8)
`email` VARCHAR( 255 ) NOT NULL ,        # VALID_UNIQUE & VALID_EMAIL
`birthday` DATETIME NOT NULL ,
PRIMARY KEY  (`id`),
UNIQUE KEY `username` (`username`),
UNIQUE KEY `email` (`email`)
) TYPE = MYISAM ;

Output ==============================================================
>>: validake users.sql
var $validate = array(
  'id' => array(
    'VALID_NUMBER' => array('rule' => 'VALID_NUMBER',
      'message' => 'id must be VALID_NUMBER'),
    'maxlength' => array('rule' => array('maxlength','10'),
      'message' => 'id must be no greater than 10 characters long'),
    'VALID_NOT_NULL' => array('rule' => 'VALID_NOT_NULL',
      'message' => 'id must be VALID_NOT_NULL'),
    'VALID_UNIQUE' => array('rule' => 'VALID_UNIQUE',
      'message' => 'email must be VALID_UNIQUE')
  ),
  'username' => array(
    'maxlength' => array('rule' => array('maxlength','40'),
      'message' => 'username must be no greater than 40 characters
long'),
    'VALID_NOT_NULL' => array('rule' => 'VALID_NOT_NULL',
      'message' => 'username must be VALID_NOT_NULL'),
    'VALID_UNIQUE' => array('rule' => 'VALID_UNIQUE',
      'message' => 'username must be VALID_UNIQUE'),
    'alphaNumeric' => array('rule' => 'alphaNumeric',
      'message' => 'username must be alphaNumeric')
  ),
  'password' => array(
    'maxlength' => array('rule' => array('maxlength','40'),
      'message' => 'password must be no greater than 40 characters
long'),
    'VALID_NOT_NULL' => array('rule' => 'VALID_NOT_NULL',
      'message' => 'password must be VALID_NOT_NULL'),
    'minLength(8)' => array('rule' => array('minLength','8'),
      'message' => 'password must be minLength(8)')
  ),
  'email' => array(
    'maxlength' => array('rule' => array('maxlength','255'),
      'message' => 'email must be no greater than 255 characters
long'),
    'VALID_NOT_NULL' => array('rule' => 'VALID_NOT_NULL',
      'message' => 'email must be VALID_NOT_NULL'),
    'VALID_UNIQUE' => array('rule' => 'VALID_UNIQUE',
      'message' => 'email must be VALID_UNIQUE'),
    'VALID_EMAIL' => array('rule' => 'VALID_EMAIL',
      'message' => 'email must be VALID_EMAIL')
  ),
  'birthday' => array(
    'VALID_DATE' => array('rule' => 'VALID_DATE',
      'message' => 'birthday must be VALID_DATE'),
    'VALID_NOT_NULL' => array('rule' => 'VALID_NOT_NULL',
      'message' => 'birthday must be VALID_NOT_NULL')
  )
);

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to