Re: Data Validation (Again, sorry)

[prev] [thread] [next] [Date index for 2004/06/29]

From: Rhesa Rozendaal
Subject: Re: Data Validation (Again, sorry)
Date: 00:49 on 29 Jun 2004
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

Data Validation (Again, sorry)
Bryon Bean 00:58 on 29 Jun 2004

Re: Data Validation (Again, sorry)
Rhesa Rozendaal 00:49 on 29 Jun 2004

Generated at 11:35 on 01 Dec 2004 by mariachi v0.52