NAME
MojoX::Validator - Validator for Mojolicious
SYNOPSIS
my $validator = MojoX::Validator->new;
# Fields
$validator->field('phone')->required(1)->regexp(qr/^\d+$/);
$validator->field([qw/firstname lastname/])
->each(sub { shift->required(1)->length(3, 20) });
# Groups
$validator->field([qw/password confirm_password/])
->each(sub { shift->required(1) });
$validator->group('passwords' => [qw/password confirm_password/])->equal;
# Conditions
$validator->field('document');
$validator->field('number');
$validator->when('document')->regexp(qr/^1$/)
->then(sub { shift->field('number')->required(1) });
$validator->validate($values_hashref);
my $errors_hashref = $validator->errors;
my $validated_values_hashref = $validator->values;
DESCRIPTION
Data validator. Validates only the data. NO form generation, NO javascript generation, NO other stuff that does something else. Only data validation!
FEATURES
* Validates data that is presented as a hash reference
* Multiple values
* Field registration
* Group validation
* Conditional validation
CONVENTIONS
* A value is considered empty when its value is B<NOT> C<undef>, C<''> or
contains only spaces
* If a value is not required and during validation is empty there is B<NO>
error
* If a value is passed as an array reference and an appropriate field is
not multiple, than only the first value is taken, otherwise every value of
the array reference is checked.
ATTRIBUTES
trim
Trim field values. ON by default.
METHODS
new
my $validator = MojoX::Validator->new;
Created a new MojoX::Validator object.
clear_errors
$validator->clear_errors;
Clear errors.
field
$validator->field('foo'); # MojoX::Validator::Field object is returned
$validator->field('foo'); # Already created field object is returned
$validator->field(qw/foo bar baz/); # MojoX::Validator::Bulk object is returned
$validator->field([qw/foo bar baz/]); # MojoX::Validator::Bulk object is returned
When a single value is passed creates MojoX::Validator::Field object or returns an already created field object.
When an array or an array reference is passed returns MojoX::Validator::Bulk object. You can call each
method to apply setting to multiple fields.
$validator->field(qw/foo bar baz/)->each(sub { shift->required(1) });
group
$validator->field(qw/foo bar/)->each(sub { shift->required(1) });
$validator->group('all_or_none' => [qw/foo bar/])->equal;
Registers a group constraint that will be called on group of fields. If group validation failes the errors
hashref will have the group name with an appropriate error message, NOT fields' names.
when
$validator->field('document');
$validator->field('number');
$validator->when('document')->regexp(qr/^1$/)
->then(sub { shift->field('number')->required(1) });
Registers a condition that is called when some conditions are met. You can do whatever you want in condition's callback. Validation will be remade.
validate
$validator->validate({a => 'b'});
$validator->validate({a => ['b', 'c']});
$validator->validate({a => ['b', 'c'], b => 'd'});
Accepts and validated a hash reference that represents data that is being validated. Hash values can be either a SCALAR
value or an ARRAREF
value, which means that a field has multiple values. In case of an array reference, it is checked if a field can have multiple values. Otherwise only the first value is accepted and returned when values
method is called.
errors
$validator->errors; # {a => 'Required'}
Returns a hash reference of errors.
values
$validator->values;
Returns a hash reference of validated values. Only registered fields are returned, that means that if some other values were passed to the validate
method they are ignored.
DEVELOPMENT
Repository
http://github.com/vti/mojox-validator
AUTHOR
Viacheslav Tykhanovskyi, vti@cpan.org
.
COPYRIGHT AND LICENSE
Copyright (C) 2010, Viacheslav Tykhanovskyi.
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.