8.5. User defined check routines
User-defined check routines can be defined with CodeDef in a file included in the code/ directory, the same as a UserTag.
CodeDef foo OrderCheck
CodeDef foo Routine <<EOR
sub {
# $ref is to Vend::Session->{'values'} hash
# $var is the passed name of the variable
# $val is current value of checked variable
my($ref, $var, $val) = @_;
my($ref, $var, $val) = @_;
if($val ne 'bar') {
return (undef, $var, "The value of foo must be bar");
}
else {
return (1, $var, '');
}
}
EOF
Now you can specify in an order profile:
foo_variable=foo
Very elaborate checks are possible. The return value of the subroutine should be a three-element array, consisting of:
- the pass/fail ('1' or 'undef') status of the check;
- the name of the variable which was checked;
- a standard error message for the failure, in case a custom one has not been specified in the order profile.
The latter two elements are used by the [error] tag for on-screen display of form errors. The checkout page of the Foundation demo includes examples of this.