NAME

Mojolicious::Plugin::Vparam - Mojolicious plugin validator for GET/POST data.

SYNOPSIS

# Get one parameter
my $param1 = $self->vparam('date' => 'datetime');
# Or more syntax
my $param2 = $self->vparam('page' => {type => 'int', default => 1});
# Or more simple syntax
my $param2 = $self->vparam('page' => 'int', default => 1);

# Get many parameters
my %params = $self->vparams(
    # Simple syntax
    name        => 'str',
    password    => qr{^\w{,32}$},
    myparam     => sub {
        my ($self, $param) = @_;
        return ($param eq 'ok') ?1 :0;
    },

    # More syntax
    from        => { type => 'date', default => '' },
    to          => { type => 'date', default => '' },
    id          => { type => 'int' },
    money       => { regexp => qr{^\d+(?:\.\d{2})?$} },
    myparam     => { post => sub {
        my ($self, $param) = @_;
        return ($param eq 'ok') ?1 :0;
    } },
    isa         => { type => 'bool', default => 0 },
);

# Same as vparams but auto add some more params for table sorting/paging
my %filters = $self->vsort(
    -sort       => ['name', 'date', ...],

    ...
);

# Get a errors hash by params name
my %errors = $self->verrors;

DESCRIPTION

This module use simple paramters types str, int, email, bool, etc. to validate. Instead of many other modules you not need add specific validation subs or rules. Just set parameter type. But if you want sub or rule you can do it too.

METHODS

vsort

Method vsort automatically add some keys.

page

Page number $PARAM_PAGE. Default: 1.

oby

Column number for sorting $PARAM_ORDER_BY. Default: 1.

ods

Sort order $PARAM_ORDER_DEST. Default: ASC.

rws

Rows on page

KEYS

You can set a simple mode as in exapmple or full mode. Full mode keys:

default

Default value. Default: undef.

regexp $mojo, $regexp

Valudator regexp by $regexp.

pre $mojo, &sub

Incoming filter sub. Used for primary filtration: string length and trim, etc. Result will be used as new param value.

valid $mojo, &sub

Validation sub. Return 1 if valid, else 0.

post $mojo, &sub

Out filter sub. Used to modify value for use in you program. Usually used to bless in some object. Result will be used as new param value.

type

Parameter type. If set then some filters will be apply.

int str date time datetime money bool email url phone

After apply all type filters, regexp and post filters will be apply too if set.

RESERVED KEYS

-sort

Arrayref for sort column names. Usually not all columns visible for users and you need convert column numbers in names. This also protect you SQL queries from set too much or too low column number.

date_parse $str

Get a string and return DateTime or undef. Have a hack for parse Russian data and time.

clean_phone $phone, $country, $region

Clear phones. Fix first local digit 8 problem.

Return <undef> if phome not correct

AUTHORS

Dmitry E. Oboukhov <unera@debian.org>, Roman V. Nikolaev <rshadow@rambler.ru>

COPYRIGHT

Copyright (C) 2011 Dmitry E. Oboukhov <unera@debian.org> Copyright (C) 2011 Roman V. Nikolaev <rshadow@rambler.ru>

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.