Data validation
[prev]
[thread]
[next]
[Date index for 2004/11/09]
Hi all,
We ran across an interesting situation today at work. During creation
of an object (Class->create($data)) we wanted to make sure that
required params were in $data. After a bit of searching the docs we
ran across validate_column_values(), and thought that would be a good
place to do the validation. Well, it's not because
validate_column_values() can be called multiple times for a single
call to create() because we have has_a relationships on date columns.
The idea is to check the data being passed to create() and die if
something is not present. But since it's called multiple times, with
different data each time, we have to do ugly hacks. So when create()
calls validate_column_values() it's passed the original data, but when
it gets called the second time from the has_a trigger it only has the
value(s) from the trigger.
So validate() gets called for the original create() call, but also by
the the before_create trigger set by the has_a relationship. We wanted
to die if create() was missing some required params, but with
validate() being called multiple times we can't reliably figure out
when to check the values and when to pass without using a nasty hack
like checking caller() to see who called the method.
The docs imply that validate_column_values() is the right place to do
this validation, but the implementation makes this rather messy. How
do other people do validation? In my perfect world I would be able to
setup my validation in one place - my CDBI classes. This metadata
would be passed along to my CGI::Application apps, which would use it
in conjunction with Data::FormValidator to validate form params.
Our current setup:
CDBI 0.92
package Contract;
# setup columns
__PACKAGE__->columns(qw(id name ourdate));
__PACKAGE__->has_a('ourdate' => 'My::Date');
*research happens*
Looks like we can upgrade to 0.95, so I'll definitely look into that.
What about 0.96 requires 5.6+ functionality. If it's relatively easy
to do and I can downgrade features gracefully, I'll provide a patch so
later versions can work with our ancient perl version.
Drew
--
----------------------------------------------------------------
Drew Taylor * Web development & consulting
Email: drew@xxxxxxxxxx.xxx * Site implementation & hosting
Web : www.drewtaylor.com * perl/mod_perl/DBI/mysql/postgres
----------------------------------------------------------------
|
(message missing)
|
|
|
Data validation
Drew Taylor 20:39 on 09 Nov 2004
|