Hi all!

Attached is a small patch for HTML/FormFu/Constraint.pm which allows to
use a callback for "when" checks inside a constraint. This should allow
more complex control (i.e. based on multiple fields, external data, etc...)
on when the constraint is applied:

my $is_blast = sub {
     my $params = shift;

     return 0 if $params->{isblastocyst} == 1 && $params->{old} == 1;

     # Closure-style
     return 0 if $c->model('Dbs::Atoms')->find($params->{old});

     return 1;
}

[...]

{
     name    => 'embryos_day5',
     type    => 'Text',
     constraints => [{
         type    => 'Number',
         when    => {
             callback => $is_blast,
         },
     }],
},

I'll provide tests if patch is considered a good idea to merge. ;-)

Thanks,
Michele.

--
Michele Beltrame
http://www.cattlegrid.info/
ICQ 76660101 - MSN [EMAIL PROTECTED]

--- home/mb/devel/formfu/trunk/HTML-FormFu/lib/HTML/FormFu/Constraint.pm 
+++ u/www/cheiron/cryo/Cryo/lib/HTML/FormFu/Constraint.pm 
@@ -107,29 +107,38 @@
     # check type of 'when'
     croak "Parameter 'when' is not a hash ref" if ref $when ne 'HASH';
 
-    # field must be defined
+    # field or callback must be defined
     my $when_field = $when->{field};
-    croak "Parameter 'field' is not defined" if !defined $when_field;
-
-    # nothing to constrain if field doesn't exist
-    my $when_field_value = $params->{$when_field};
-    return 0 if !defined $when_field_value;
-
-    # a compare value must be defined
-    my @values;
-    my $compare_value = $when->{value};
-    push @values, $compare_value if defined $compare_value;
-    my $compare_values = $when->{values};
-    push @values, @$compare_values if ref $compare_values eq 'ARRAY';
-    croak "Parameter 'value' or 'values' are not defined" unless @values;
-
-    # determine if condition is fullfilled
-    my $fullfilled = grep { $when_field_value eq $_ } @values;
-
-    # invert when condition if asked for
-    $fullfilled = $when->{not} ? !$fullfilled : $fullfilled;
-
-    return $fullfilled;
+    my $when_callback = $when->{callback};
+    croak "Parameter 'field' or 'callback' is not defined"
+        if !defined $when_field && !defined $when_callback;
+
+    # Callback will be the preferred thing
+    if ( $when_callback ) {
+        return $when_callback->($params);
+    }
+
+    else {
+        # nothing to constrain if field doesn't exist
+        my $when_field_value = $params->{$when_field};
+        return 0 if !defined $when_field_value;
+
+        # a compare value must be defined
+        my @values;
+        my $compare_value = $when->{value};
+        push @values, $compare_value if defined $compare_value;
+        my $compare_values = $when->{values};
+        push @values, @$compare_values if ref $compare_values eq 'ARRAY';
+        croak "Parameter 'value' or 'values' are not defined" unless @values;
+
+        # determine if condition is fullfilled
+        my $fullfilled = grep { $when_field_value eq $_ } @values;
+
+        # invert when condition if asked for
+        $fullfilled = $when->{not} ? !$fullfilled : $fullfilled;
+
+        return $fullfilled;
+    }
 }
 
 1;
@@ -259,6 +268,10 @@
   value: expected value in the form field 'field'
   values: Array of multiple values, one must match to fullfill the condition
   not: inverse the when condition - value(s) must not match
+  callback: a callback can be supplied to perform complex checks. An hashref
+    of all parameters is passed. In this case all other keys are ignored,
+    including not. Return a true value for the constraint to be valid or
+    a false value to not apply it.
 
 =head1 CORE CONSTRAINTS
 
_______________________________________________
HTML-FormFu mailing list
HTML-FormFu@lists.scsys.co.uk
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu

Reply via email to