Shawn Boles Fri Jul 23 00:36:03 -0400 2010

Subject: Problem with $validates_presence_of && $validates_format_of

Howdy!

I've been working off and on with PHPActiveRecord on an application I've been tinkering with. PHPAR is the database abstraction layer I've been waiting for.

I'm still tinkering with Validations. And I've run into an oops.

Here's my User validations array:

static $validates_presence_of = array(
array('password'),
array('handle'),
array('name'),
array('privateemail'),
);
static $validates_format_of = array(
array('privateemail', 'with' => '/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/'),
array('publicemail', 'with' => '/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[
][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/'),
);

I only require a "privateemail", but I'd like to validate the public email against the same regex (stolen, ruthlessly, from the validations page). However, if the user doesn't supply a publicemail, there is an error!

What I would expect is, if I have a validation against something, but DON'T have a validates_presence_of against it, not having that attribute would be ignored.

Is this a bug? Or am I doin' it wrong?


Shawn Boles Fri Jul 23 23:34:39 -0400 2010

So I guess this is the impoper way to set up validators for a class?

The example above is from a registration form; I like the validators because I hate writing validators. I've got Jquery validators which quite handily match up with the validators on the PHPAR side (I was even writing an automatic generator, but that ended up going way out of scope and got shelved for now).

For example, with the JQuery validator, I can flag the first email as "required email" and the second as "email". When I submit the form with an invalid email address in the first field, but the second field empty, I only get complaints from the first field, as the second isn't required.

This is the functionality I'd like to see PHPAR do... but it's entirely possible that I completely broke the way you're supposed to set up validators. TBH, I didn't pay much attention to validators in past tinkerings, but I decided to write this codebase "right" from square 1.

For reference, the Schema for my table is:

CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'User ID',
`active` tinyint(1) NOT NULL DEFAULT '0',
`password` char(40) COLLATE armscii8_bin NOT NULL COMMENT 'Hex SHA Hash',
`handle` varchar(255) COLLATE armscii8_bin NOT NULL COMMENT 'Username',
`name` varchar(255) COLLATE armscii8_bin NOT NULL COMMENT 'Real Name',
`title` varchar(255) COLLATE armscii8_bin DEFAULT NULL COMMENT 'Custom title',
`privateEmail` varchar(255) COLLATE armscii8_bin NOT NULL COMMENT 'Private email address',
`publicEmail` varchar(255) COLLATE armscii8_bin DEFAULT NULL COMMENT 'A public email address',
`aim` varchar(16) COLLATE armscii8_bin DEFAULT NULL COMMENT 'AOL Instant Messenger screenname',
`msn` varchar(255) COLLATE armscii8_bin DEFAULT NULL COMMENT 'MSN Screenname',
`jabber` varchar(255) COLLATE armscii8_bin DEFAULT NULL COMMENT 'Jabber screenname',
`yahoo` varchar(255) COLLATE armscii8_bin DEFAULT NULL COMMENT 'Y! Instant Messenger screenname',
`moreInfo` text COLLATE armscii8_bin COMMENT 'Option more information.',
PRIMARY KEY (`id`),
UNIQUE KEY `handle` (`handle`)
) ENGINE=MARIA DEFAULT CHARSET=armscii8 COLLATE=armscii8_bin PAGE_CHECKSUM=1 COMMENT='User identification table.' AUTO_INCREMENT=4 ;

I'm running "PHP 5.3.2-1ubuntu4.2 with Suhosin-Patch" and MariaDB: "5.1.47-MariaDB-mariadb82-log".

Thanks for your time. I go back to tinkering. Most likely I'll depend on my front end validations for now while I'm rapidly-prototyping then insert validations in when this is figured out.

Kien La Sun Jul 25 16:50:39 -0400 2010

Try using the allow_blank option:

array('publicemail',
'with' => '/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/',
'allow_blank' => true
)

There's also an allow_null option.

(1-2/2)