| ZForm User Manual | ||
|---|---|---|
| <<< Previous | Next >>> | |
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); |
| <<< Previous | Home | Next >>> |
| Custom Error Messages | Form Fragments |