Validation

ZForm::FieldValidator

This is a quick example that shows how to use ZForm::FieldValidator objects to add moreNot complete

Example4.pm

package Example4;
use base ZForm;

use ZForm::FieldValidator;

sub setup {
    my $self = shift();

    my $no_sql = new ZForm::FieldValidator(type => 'no_sql');
    my $integer = new ZForm::FieldValidator(type => 'integer');

    # Define the form
    my $fields = [
                  {'rm' => {
                       'type' => 'hidden',
                       'attr' => {
                           'default' => 'display_data'}}},

                  {'integer' => {
                       'type' => 'textfield',
                       'label' => 'Enter an integer',
                       'validators' => [$no_sql,
                                        $integer],
                       'attr' => {
                           'size' => '30'}}},

                  ];

    $self->form_def($fields);
}

1;

example4.cgi

#!/usr/bin/perl

use lib '.';
use strict;
use warnings;
use CGI;
use Example4;

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

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

# Placeholder for output
my $output = '';

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);

Deriving new ZForm::Validator Classes

This is the best way to add new types of validation as opposed to using the builtin regex method in ZForm::Validator. The benefit is that it is easy to define your own error message.

Clever example should go here.

Overridding the ZForm validate method

Sometimes, you need to extend the basic validation logic. In these cases, you can override the ZForm validate method with your own.

Clever example should go here.