Custom Error Messages

The error messages that you have seen are all built into ZForm. The module that defines these error messages is ZForm::ErrorMessages. If you would like to change the error messages, derive a new class from ZForm::ErrorMessages and pass in an instance to your ZForm derived class. Example:

CustomerErrors.pm:

package CustomErrors;

use base ZForm::ErrorMessages;

sub required {
    return "Maybe you should fill out this field";
}

1;

example3.cgi changes a little bit:

#!/usr/bin/perl

use lib '.';
use strict;
use warnings;
use CGI;
use Example3;
use CustomErrors;

# Create a new cgi object
my $q = new CGI;

# Create a custom error handler
my $error_handler = new CustomErrors;

# Create an instance of our form and attach the cgi object
my $f = new Example3(cgi => $q,
                     error_handler => $error_handler);

# Placeholder for output
my $output = '';

my $title;
my $descr;

if(defined($q->param('rm')) &&
   $q->param('rm') eq 'display_data') {
    # User clicked submit, so validate the form
    if($f->validate()) {
        # It validated! Display the data
        $output = $f->display_data_only();
    } else {
        # Didn't validate, so display the form with errors
        $output = $f->display();
    }
} else {
    # The user has not yet clicked submit, so display the empty form
    $output = $f->display();
}

# Display the output
print($q->header, $output);

Try submitting the form without any values. Notice how our custom error message shows up.