validation when setting a column value

41 views
Skip to first unread message

dstroma

unread,
Mar 23, 2009, 5:05:54 PM3/23/09
to Rose::DB::Object
Hi,

Currently when I create an instance of a Rose::DB::Object and set a
column value, it validates it and throws an exception immediately if
the value is invalid. Is there a way to only validate on save or
otherwise explicity? This behavior complicates my app because I was
planning on having my controllers create new objects with raw form
input as column values, with validation occuring in my models (RDBO
subclasses). With the current behavior I need validation in two places.

John Ingram

unread,
Mar 23, 2009, 5:09:48 PM3/23/09
to rose-db...@googlegroups.com
init_error_mode is 'fatal' by default. You can set it to 'return' in
a Rose::DB::Object::Metadata subclass:

sub init_error_mode { 'return' }

John

John Siracusa

unread,
Mar 23, 2009, 5:56:20 PM3/23/09
to rose-db...@googlegroups.com

The database validation may be looser than your app's validation. If
they're an exact match for your right now, you're lucky, but I
wouldn't plan your design around that being true forever. My
suggestion is to use a form handler package of some kind to do your
application-level data validation, only passing data onto RDBO after
you're sure it's valid. To avoid repeating validation code in the
cases where the db and app constraints match, you can automatically
generate your form constraints based on your RDBO column metadata. (I
think there are a few CPAN packages for doing this.)

(If you want to use RDBO as an "unvalidated data container" to pass
values around inside your app, then I suppose you could avoid putting
any constraints on your columns (length, min/max, etc.) and let the
database do all the validation and data rejection.)

Finally, if your primary concern is with string lengths, this option
may help you keep your column constraints while also not dying on bad
input:

http://search.cpan.org/dist/Rose-DB-Object/lib/Rose/DB/Object/Metadata/Column/Scalar.pm#overflow

-John

dstroma

unread,
Mar 23, 2009, 6:02:04 PM3/23/09
to Rose::DB::Object
Thanks for the fast reply. That helps but is not quite what I want,
which is to only want to affect the error handling for the accessor/
mutator methods, not all methods of a Rose::DB::Object. What I really
am trying to do is forcibly set a column value even if it's not valid.
Something like :

$object->timestamp('asdflkjsdj');
unless ($object->save) {
print 'Sorry, ', $object->timestamp, ' is not a valid date';
}

I guess this may not be possible with the way Rose is designed. Any
suggestions?
> > subclasses). With the current behavior I need validation in two places.- Hide quoted text -
>
> - Show quoted text -

dstroma

unread,
Mar 23, 2009, 6:08:20 PM3/23/09
to Rose::DB::Object
Hi Mr. Siracusa, I was working on my reply to the other John as you
posted.

The database validation that Rose does is indeed looser than my
application validation. But I want all the validation to be done by my
objects themselves, rather than before the objects are created. (This
is the way Rails does it).

In my object classes I override the save() method to do my application
validation before the actual save occurs. Even if I change the error
handling to return as John Ingram suggested, when my own validation is
done the columns that Rose found invalid will be empty and I will not
be able to give friendly information back to the user.


On Mar 23, 5:56 pm, John Siracusa <sirac...@gmail.com> wrote:
> http://search.cpan.org/dist/Rose-DB-Object/lib/Rose/DB/Object/Metadat...
>
> -John
Reply all
Reply to author
Forward
0 new messages