Re: Data Validation (Again, sorry)
[prev]
[thread]
[next]
[Date index for 2004/06/29]
Bryon Bean wrote:
> Hi,
>
[snip]
> package Music;
> use Error;
> ...
> sub _croak {
> my ($self, $message, %info) = @_;
> Error->throw(-text => $message, %info);
> }
This works. I use a subclassed Error though, which sorta functions like
a more specific exception class. I use CDBI in the code below, but any
namespace will do. You might consider My::Error, My::Error::DB etc. to
specialize exceptions.
package CDBI::Error;
use base qw/Error/;
1;
in the _croak:
use CDBI::Error;
CDBI::Error->throw(...)
> package Music::CD;
> ...
> __PACKAGE__->constrain_column( price => qr/\d{2}\.\d{2}/ );
That's all that's needed.
> package SOME_CGI_APP
> use base 'CGI::Application'
use CDBI::Error qw/try/;
> ...
>
> sub create_cd {
> ...
try {
> my $cd = Music::CD->create( ..., price => '$14' );
catch CDBI::Error with {
my $E = shift;
# $E->text contains the error message
# $E->data is a hashref filled by CDBI
# where each key corresponds to the fields
# that didn't pass the constraint
# I usually just pass the keys to the template with a
# true value, so I can use them in a <tmpl_if> block
$template->param( "err_$_" => 1 )
foreach keys %{$E->data};
}
> }
So Error's try - catch system is the answer to your question :-)
Rhesa
|
|
Re: Data Validation (Again, sorry)
Rhesa Rozendaal 00:49 on 29 Jun 2004
|