<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># NAME

FormValidator::Lite - lightweight form validation library

# SYNOPSIS

    use FormValidator::Lite;

    FormValidator::Lite-&gt;load_constraints(qw/Japanese/);

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

    # in your template
    &lt;ul&gt;
    ? for my $msg ($validator-&gt;get_error_messages) {
        &lt;li&gt;&lt;?= $msg ?&gt;&lt;/li&gt;
    ? }
    &lt;/ul&gt;

# 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' =&gt; sub {
        return $_ % 2 ? 0 : 1;
    };
    

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

    1;

And in your controller :

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

    my $validator = FormValidator::Lite-&gt;new(...);
    $validator-&gt;set_message_data(...);
    $validator-&gt;check(
        some_param =&gt; [ 'UINT', 'IS_EVEN', ['IS_GREATER_THAN' =&gt; 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

- my $validator = FormValidator::Lite-&gt;new($q);

    Create a new instance.

    The constructor takes a mandatory argument `$q` that is a query-like 
    object such as Apache::Request, CGI.pm, Plack::Request. The object MUST have
    a `$q-&gt;param` method.

- $validator-&gt;query()
- $validator-&gt;query($query)

    Getter/Setter for the query attribute.

- $validator-&gt;check(@specs\_array)

    Validate the query against a set of specifications defined in the
    `@specs_array` argument. In the most common case, the array is a sequence
    of pairs : the first item is the parameter name and the second item is an
    array reference with a list of constraint rules to apply on the query's value
    for the parameter.

        my $res = $validator-&gt;check(
            name      =&gt; [qw/NOT_NULL/],
            name_kana =&gt; [qw/NOT_NULL KATAKANA/],
            {mails =&gt; [qw/mail1 mail2/]} =&gt; ['DUPLICATION'],
        );

    In the above example _name_ is a parameter. _NOT\_NULL_, _KATAKANA_ and
    _DUPLICATION_ are the names of the constraints.

- $validator-&gt;is\_error($key)

    Return true value if there is an error for the `$key` parameter.

- $validator-&gt;is\_valid()

    Return true value if `$validator` didn't detect any error.

    This is the same as `!$validator-&gt;has_error()`.

- $validator-&gt;has\_error()

    Return true value if `$validator` detects error.

    This is the same as `!$validator-&gt;is_valid()`.

- $validator-&gt;set\_error($param, $rule\_name)

    Manually set a new error for the parameter named `$param`. The rule's name
    is `$rule_name`.

- $validator-&gt;errors()

    Return all the errors as a hash reference where the keys are the parameters
    and the values are a hash reference with the failing constraints.
        

        {
            'foo' =&gt; { 'NOT_NULL' =&gt; 1, 'INT' =&gt; 1 },
            'bar' =&gt; { 'EMAIL' =&gt; 1, },
        }

- $validator-&gt;load\_constraints($name)

        $validator-&gt;load_constraints("DATE", "Email");

        # or load your own constraints
        $validator-&gt;load_constraints("+MyApp::FormValidator::Lite::Constraint");

    You can also load the constraints during import :

        use FormValidator::Lite qw/Date Email/;

    Load constraint components named `"FormValidator::Lite::Constraint::${name}"`.

- $validator-&gt;load\_function\_message($lang)

        $validator-&gt;load_function_message('ja');

    Load function message file.

    Currently, [FormValidator::Lite::Messages::ja](http://search.cpan.org/perldoc?FormValidator::Lite::Messages::ja) and
    [FormValidator::Lite::Messages::en](http://search.cpan.org/perldoc?FormValidator::Lite::Messages::en) are available.

- $validator-&gt;set\_param\_message($param =&gt; $message, ...)
    

        $validator-&gt;set_param_message(
            name =&gt; 'Your Name',
        );

    Add a message-friendly description for the parameter.

- $validator-&gt;set\_message("$param.$func" =&gt; $message)

        $v-&gt;set_message('zip.jzip' =&gt; 'Please input correct zip number.');

    Set an error message for a given $param and $func pair.

- $validator-&gt;set\_message\_data({ message =&gt; $msg, param =&gt; $param, function =&gt; $function })

        $v-&gt;set_message_data(YAML::Load(&lt;&lt;'...'));
        ---
        message:
          zip.jzip: Please input correct zip number.
        param:
          name: Your Name
        function:
          not_null: "[_1] is empty"
          hiragana: "[_1] is not Hiragana"
        ...

    Set the error message map. In the 'function' and 'message' sections,
    `[_1]` will be replaced by `build_message` with the description of
    the failing parameter provided in the 'param' section.

- `$validator-&gt;build_message($tmpl, @args)`

    replace \[\_1\] in `$tmpl` by `@args`.

    Setup error message map.

- `$validator-&gt;set_message("$param.$func" =&gt; $message)`

        $v-&gt;set_message('zip.jzip' =&gt; 'Please input correct zip number.');

    Note that it will void any previous calls to `load_function_message`,
    `set_message` or `set_param_message`.

- my @errors = $validator-&gt;get\_error\_messages()
- my $errors = $validator-&gt;get\_error\_messages()

    Get all the error messages for the query. This method returns an array in list
    context and an array reference otherwise.

- my $msg = $validator-&gt;get\_error\_message($param =&gt; $func)

    Generate the error message for parameter `$param` and function
    `$func`.

- my @msgs = $validator-&gt;get\_error\_messages\_from\_param($param)

    Get all the error messages for the parameter `$param`.

# 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 &lt;tokuhirom AAJKLFJEF@ gmail.com&gt;

# THANKS TO

craftworks

nekokak

tomi-ru

# SEE ALSO

[FormValidator::Simple](http://search.cpan.org/perldoc?FormValidator::Simple), [Data::FormValidator](http://search.cpan.org/perldoc?Data::FormValidator), [HTML::FormFu](http://search.cpan.org/perldoc?HTML::FormFu)

# LICENSE

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