Вячеслав Тихановский > Input-Validator > Input::Validator

Download:
Input-Validator-0.001005.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.001005   Source  

NAME ^

Input::Validator - Input Validator

SYNOPSIS ^

    my $validator = Input::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 $pass_error = $validator->group('passwords')->error;
    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 C<undef>, C<''> or
    contains only spaces.

    If a value is not required and during validation is empty there is B<NO>
    error.

    If explicit is set to true, then all values not explicitly required
    generate an 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 ^

explicit

Causes errors to be generated when unknown parameters exist.

has_unknown_params

Unknown parameters exist in validated parameter hashref.

messages

    my $validator =
      Input::Validator->new(
        messages => {REQUIRED => 'This field is required'});

Replace default messages.

trim

Trim field values. ON by default.

METHODS ^

new

    my $validator = Input::Validator->new;

Create a new Input::Validator object.

clear_errors

    $validator->clear_errors;

Clear errors.

field

    $validator->field('foo');               # Input::Validator::Field object is returned
    $validator->field('foo');               # Already created field object is returned

    $validator->field(qw/foo bar baz/);     # Input::Validator::Bulk object is returned
    $validator->field([qw/foo bar baz/]);   # Input::Validator::Bulk object is returned

When a single value is passed create Input::Validator::Field object or return an already created field object.

When an array or an array reference is passed return Input::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;

Register a group constraint that will be called on group of fields. If group validation fails 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) });

Register 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'});

Accept and validate a hash reference that represents data that is being validated. Input 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.

error

    $validator->error(foo => 'bar');

Set a custom error.

errors

    $validator->errors; # {a => 'Required'}

Return a hash reference of errors.

has_errors

    $validator->has_errors;

Check if there are any errors.

values

    $validator->values;

Return 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.

all_values

    $validator->all_values;

Return a hash reference of all values. Only registered fields are returned. Useful for displaying wrong values for user to reenter.

DEVELOPMENT ^

Repository

    http://github.com/vti/input-validator

AUTHOR ^

Viacheslav Tykhanovskyi, vti@cpan.org.

CREDITS ^

In alphabetical order:

Alex Voronov

Anatoliy Lapitskiy

Glen Hinkle

Marcus Ramberg

Naoya Ito

Yaroslav Korshak

COPYRIGHT AND LICENSE ^

Copyright (C) 2011, 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.

syntax highlighting: