NAME

Mojolicious::Validator::Validation - Perform validations

SYNOPSIS

use Mojolicious::Validator;
use Mojolicious::Validator::Validation;

my $validator = Mojolicious::Validator->new;
my $validation
  = Mojolicious::Validator::Validation->new(validator => $validator);

DESCRIPTION

Mojolicious::Validator::Validation performs validations. Note that this module is EXPERIMENTAL and might change without warning!

ATTRIBUTES

Mojolicious::Validator::Validation implements the following attributes.

input

my $input   = $validation->input;
$validation = $validation->input({});

Data to be validated.

output

my $output  = $validation->output;
$validation = $validation->output({});

Validated data.

topic

my $topic   = $validation->topic;
$validation = $validation->topic('foo');

Current validation topic.

validator

my $validator = $validation->validator;
$validation   = $validation->validator(Mojolicious::Validator->new);

Mojolicious::Validator object this validation belongs to.

METHODS

Mojolicious::Validator::Validation inherits all methods from Mojo::Base and implements the following new ones.

check

$validation = $validation->check('size', 2, 7);

Perform validation check.

error

$validation = $validation->error('This went wrong.');

Set custom error message for next validation check or topic change.

$validation->optional('name')
  ->error('Name needs to be between 3 and 9 characters long.')->size(3, 9);

errors

my $collection = $validation->errors('foo');

Return Mojo::Collection object containing all error messages for failed validation checks.

has_data

my $success = $validation->has_data;

Check if input is available for validation.

has_error

my $success = $validation->has_error;
my $success = $validation->has_error('foo');

Check if validation resulted in errors, defaults to checking all fields.

is_valid

my $success = $validation->is_valid;
my $success = $validation->is_valid('foo');

Check if validation was successful and field has a value, defaults to checking the current topic.

optional

$validation = $validation->optional('foo');

Change validation topic.

param

my @names       = $c->param;
my $foo         = $c->param('foo');
my @foo         = $c->param('foo');
my ($foo, $bar) = $c->param(['foo', 'bar']);

Access validated parameters.

required

$validation = $validation->required('foo');

Change validation topic and make sure a value is present.

CHECKS

In addition to the methods above, you can also call validation checks provided by Mojolicious::Validator on Mojolicious::Validator::Validation objects, similar to check.

$validation->required('foo')->size(2, 5)->regex(qr/^[A-Z]/);
$validation->optional('bar')->equal_to('foo');
$validation->optional('baz')->in(qw(test 123));

SEE ALSO

Mojolicious, Mojolicious::Guides, http://mojolicio.us.