The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

FormValidator::Lite - lightweight form validation library

SYNOPSIS

use FormValidator::Lite;

FormValidator::Lite->load_constraints(qw/Japanese/);

my $q = CGI->new();
my $validator = FormValidator::Lite->new($q);
$validator->load_function_message('en');
my $res = $validator->check(
    name => [qw/NOT_NULL/],
    name_kana => [qw/NOT_NULL KATAKANA/],
    {mails => [qw/mail1 mail2/]} => ['DUPLICATION'],
);
if ( ..... return_true_if_error() ..... ) {
    $validator->set_error('login_id' => 'DUPLICATION');
}
if ($validator->has_error) {
    ...
}

# in your template
<ul>
? for my $msg ($validator->get_error_messages) {
    <li><?= $msg ?></li>
? }
</ul>

DESCRIPTION

FormValidator::Lite is a simple, fast implementation for form validation.

IT'S IN BETA QUALITY. API MAY CHANGE IN THE FUTURE.

HOW TO WRITE YOUR OWN CONSTRAINTS

Create your own constraint package as such :

package MyApp::Validator::Constraint;
use strict;
use warnings;
use FormValidator::Lite::Constraint;


rule 'IS_EVEN' => sub {
    return $_ % 2 ? 0 : 1;
};


rule 'IS_GREATER_THAN' => sub {
    my ($min) = @_;
    return $_ >= $min;
}
alias 'IS_GREATER_THAN' => 'IS_BIGGER_THAN';


1;

And in your controller :

use FormValidator::Lite qw("+MyApp::Validator::Constraint");


my $validator = FormValidator::Lite->new(...);
$validator->set_message_data(...);
$validator->check(
    some_param => [ 'UINT', 'IS_EVEN', ['IS_GREATER_THAN' => 42] ],
);

When defining a rule keep in mind that the value for the parameter comes from $_ and the additional arguments defined in your validation specifications come from @_.

METHODS

WHY NOT FormValidator::Simple?

Yes, I know. This module is very similar with FV::S.

But, FormValidator::Simple is too heavy for me. FormValidator::Lite is fast!

Perl: 5.010000
FVS: 0.23
FVL: 0.02
                        Rate FormValidator::Simple   FormValidator::Lite
FormValidator::Simple  353/s                    --                  -75%
FormValidator::Lite   1429/s                  304%                    --

AUTHOR

Tokuhiro Matsuno

THANKS TO

craftworks

nekokak

tomi-ru

SEE ALSO

FormValidator::Simple, Data::FormValidator, HTML::FormFu

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.