NAME
Mojolicious::Plugin::Vparam - Mojolicious plugin validator for GET/POST data.
DESCRIPTION
Features:
* Simple syntax or full featured
* Many predefined types
* Support arrays of values
* Support HTML checkbox as bool
* Validate all parameters at once and get hash to simple use in any Model
* Manage valudation errors
* L<# Mojo::Validator::Validation> integration
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.
SYNOPSIS
# Add plugin in startup
$self->plugin('Vparam');
# Use in controller
$login = $self->vparam(login => 'str');
$passw = $self->vparam(password => 'password');
METHODS
vparam ...
Get one parameter. By default parameter is required.
# Simple get one parameter
$param1 = $self->vparam(date => 'datetime');
# Or more syntax
$param2 = $self->vparam(count => {type => 'int', default => 1});
# Or more simple syntax
$param2 = $self->vparam(count => 'int', default => 1);
vparms ...
Get many parameters as hash. By default parameters are required.
%params = $self->vparams(
# Simple syntax
name => 'str',
password => qr{^\w{,32}$},
myparam => sub { $_[1] && $_[1] eq 'ok' ? 1 : 0 } },
someone => ['one', 'two', 'tree'],
# More syntax
from => { type => 'date', default => '' },
to => { type => 'date', default => '' },
id => { type => 'int' },
money => { regexp => qr{^\d+(?:\.\d{2})?$} },
myparam => { post => sub { $_[1] && $_[1] eq 'ok' ? 1 : 0 } },
# Checkbox
isa => { type => 'bool', default => 0 },
);
vsort ...
Like vparams but add some keys to simple use with tables.
%filters = $self->vsort(
-sort => ['name', 'date', ...],
# next as vparams
...
);
- page
-
Page number. Default: 1.
You can set different name by vsort_page config parameter. If you set undef then parameter is not apply.
- rws
-
Rows on page. Default: 25.
You can set different name by vsort_rws config parameter. You can set different default by vsort_rows config parameter. If you set undef then parameter is not apply.
- oby
-
Column number for sorting. Default: 1 - in many cases first database column is primary key. If you set undef then parameter is not apply.
You can set different name by vsort_oby config parameter.
- ods
-
Sort order ASC|DESC. Default: ASC.
You can set different name by vsort_ods config parameter. If you set undef then parameter is not apply.
verror $name
Get parameter error string. Return 0 if no error.
print $self->verror('myparam') || 'Ok';
verrors
Return erorrs count in scalar context. In list context return erorrs hash.
# List context get hash
my %errors = $self->verrors;
# Scalar context get count
die 'Errors!' if $self->verrors;
vtype $name, %opts
Set new type $name if defined %opts. Else return type $name definition.
CONFIGURATION
- types
-
You can simple add you own types. Just set this parameters as HashRef with new types definition.
- vsort_page
-
Parameter name for current page number in vsort. Default: page.
- vsort_rws
-
Parameter name for current page rows count in vsort. Default: rws.
- rows
-
Default rows count for vsort_rws. Default: 25.
- vsort_oby
-
Parameter name for current order by vsort. Default: oby.
- vsort_ods
-
Parameter name for current order destination vsort. Default: ods.
- ods
-
Default order destination for vsort_rws. Default: ASC.
- phone_country
-
Phone country. Default: empty.
- phone_region
-
Phone region. Default: empty.
- date
-
Date format for strftime. Default: %F. if no format specified, return DateTime object.
- time
-
Time format for strftime. Default: %T. if no format specified, return DateTime object.
- datetime
-
Datetime format for strftime. Default: '%F %T %z'. if no format specified, return DateTime object.
- optional
-
By default all parameters are required. You can change this by set this parameter as true.
- address_secret
-
Secret for address:points signing. Format: "ADDRESS:LATITUDE,LONGITUDE[MD5]". MD5 ensures that the coordinates belong to address.
- password_min
-
Minimum password length. Default: 8.
- mojo_validator
-
Enable "# Mojo::Validator::Validation" integration.
TYPES
List of supported types:
- int
-
Signed integer
- numeric or number
-
Signed number
- money
-
Get DR::Money object for proper money operations.
- percent
-
Unsigned number: 0 <= percent <= 100.
- str
-
Trimmed text. Must be non empty if required.
- text
-
Any text. No errors.
- password
-
String with minimum length from password_min. Must content characters and digits.
- uuid
-
Standart 32 length UUID. Return in lower case.
- date
-
Get date. Parsed from many formats. See date configuration parameter for result format. See DateTime::Format::DateParse and even more.
- time
-
Get time. Parsed from many formats. See time configuration parameter for result format. See DateTime::Format::DateParse and even more.
- datetime
-
Get full date and time. Parsed from many formats. See datetime configuration parameter for result format. See DateTime::Format::DateParse and even more.
- bool
-
Boolean value. Can be used to get value from checkbox or another sources.
HTML forms do not send checbox if it checked off. You need always set default value to supress error if checkbox not checked:
$self->vparam(mybox => 'bool', default => 0);
-
Email adress.
- url
-
Get url as Mojo::Url object.
- phone
-
Phone in international format.
You can set default country phone_country and region phone_country codes. Then you users can input shortest number. But this is not work if you site has i18n.
- json
-
JSON incapsulated as form parameter.
- address
-
Location address. Two forms are parsed: string and json. Can verify adress sign to trust source.
- lon
-
Longitude.
- lat
-
Latilude.
- inn
-
RU: Taxpayer Identification Number
- kpp
-
RU: Code of reason for registration
ATTRIBUTES
You can set a simple mode as in example 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 0 if valid, else string of error.
- 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.
See L<TYPES>
After apply all type filters, regexp and post filters will be apply too if set.
- array
-
Force value will array. Default: false.
You can force values will arrays by @ prefix or array[...].
# Arrays $param1 = $self->vparam(array1 => '@int'); $param2 = $self->vparam(array2 => 'array[int]'); $param3 = $self->vparam(array3 => 'int', array => 1); # The array will come if more than one value incoming $param4 = $self->vparam(array4 => 'int');
- optional
-
By default all parameters are required. You can change this for parameter by set "optional". Then true and value is not passed validation don`t set verrors.
# Simple vparam $param6 = $self->vparam(myparam => 'int', optional => 1); # Set one in vparams %params = $self->vparams( myparam => { type => 'int', optional => 1 }, ); # Set all in vparams %params = $self->vparams( -optional => 1, param1 => 'int', param2 => 'int', );
- min
-
Check minimum parameter value.
- max
-
Check maximum parameter value.
- range
-
Check parameter value to be in range.
- in
-
Check parameter value to be in list of defined values.
RESERVED ATTRIBUTES
- -sort
-
List of column names for vsort. 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.
- -optional
-
Set default optional flag for all params in vparams and vsort;
RESTRICTIONS
* Version 1.0 invert L<valid> behavior: now checker return 0 if no error
or description string if has.
* New errors keys: orig => in, pre => out
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.