Re: Better handling of constraint failures?
[prev]
[thread]
[next]
[Date index for 2004/08/12]
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?
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
};
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}/]);
My further plans on this module is to build a CDBI::Form clone that
intgrates Class::DBI with my Data::Form module
Clayton