Re: Better handling of constraint failures?

[prev] [thread] [next] [Date index for 2004/08/12]

From: Drew Taylor
Subject: Re: Better handling of constraint failures?
Date: 19:54 on 12 Aug 2004
Clayton L. Scott wrote:

> On Thu, 12 Aug 2004, Drew Taylor wrote:
> 
> 
>>But I also have constraints for some columns in a few CDBI classes. So
>>now I have constraints in 2 places: my D::FV profiles and my CDBI class.
>>It would be nice if there were a way to "export" the constraints with my
>>CDBI classes so that I can easily integrate that with D::FV. Something like:
>>
>>my $cdbi_cons = Class->export_constraints;
>>my $dfv_profiles = {runmode1=>{required=>['foo'],
>>                                constraints=>$cdbi_cons,
>>                               }};
>># do actual D::FV validation here
>>
>>The idea here is that it's MUCH easier for me to have the D::FV failures
>>integrated with my forms than CDBI constraints. I already have glue code
>>setup so that all failures are caught and properly displayed to the
>>user. Thoughts? Did I explain myself properly?
> 
> 
> What makes it easier? The error reporting mechanism?

Yes. I've been using CDBI constraints at the database level, and DFV at 
the application level. DFV gives me nice error messages for free which I 
can easily add to my templates. It would be a lot more work (and less 
maintainable IMHO) to try to catch the errors from CDBI constraints and 
then figure out user-friendly error messages to sent to the user.


> Sitting on my harddrive at home is a suite of modules I call Data::Form
> that allows you to set up multiple validators per field in the following
> form:
> 
> $field = new Field (
>     name     => 'street',
>     label    => 'Street Address',
>     untaint  => qr/^((\w+|\s+)+)$/,
>     validate => { 'between 2 and 100 characters' => qr/^.{2,100}$/,
>                   'cannot contain V' => sub { shift =~ /V/ ? 0 : 1 },
>                  }
> );
> 
> $field->fill_in('25 rue Vanier');
> 
> unless ( $field->is_valid ){
>   @errors = $field->errors; #cannot contain V
> };

That is pretty cool. In my case though it retraces ground I've already 
covered with DFV & CDBI::FromCGI.

> I could easily excise the validating routine if anyone wants if for CDBI
> constraints. I just read that Tony added sub-based constraints to
> constrain_column based but custom error messages per constraint would also help.
> 
> #Film->constrain_column(year => qr/\d{4}/);
> Film->constrain_column(year => [ 'Year must be four digits' => qr/\d{4}/]);

Hmmm, I somehow overlooked this method. I've always just used 
add_constraint(). I do think having a nice error message to report back 
to the user is a good thing. The problem with the syntax you propose 
above is that constrain_column() currently takes a regex or arrayref of 
valid values. Seems like the code would be ugly to check if the 2nd 
valid value is a regex.

Drew
        -- 
        ----------------------------------------------------------------
Drew Taylor                 *  Web development & consulting
Email: drew@xxxxxxxxxx.xxx  *  Site implementation & hosting
Web  : www.drewtaylor.com   *  perl/mod_perl/DBI/mysql/postgres
----------------------------------------------------------------

Re: Better handling of constraint failures?
Drew Taylor 17:27 on 12 Aug 2004

Re: Better handling of constraint failures?
Clayton L. Scott 19:29 on 12 Aug 2004

Re: Better handling of constraint failures?
Drew Taylor 19:54 on 12 Aug 2004

Re: Better handling of constraint failures?
Clayton L. Scott 19:53 on 12 Aug 2004

Re: Better handling of constraint failures?
Clayton L. Scott 20:01 on 12 Aug 2004

Re: Better handling of constraint failures?
Perrin Harkins 19:51 on 12 Aug 2004

Re: Better handling of constraint failures?
Daniel Collis Puro 20:01 on 12 Aug 2004

Re: Better handling of constraint failures?
Clayton L. Scott 20:04 on 12 Aug 2004

Re: Better handling of constraint failures?
Daniel Collis Puro 20:42 on 12 Aug 2004

Re: Better handling of constraint failures?
Clayton Scott 21:49 on 12 Aug 2004

Re: Better handling of constraint failures?
Tony Bowden 13:52 on 24 Oct 2004

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