GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Input::Validator(3) User Contributed Perl Documentation Input::Validator(3)

Input::Validator - Input Validator

    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;

Data validator. Validates only the data. NO form generation, NO javascript generation, NO other stuff that does something else. Only data validation!

    Validates data that is presented as a hash reference

    Multiple values

    Field registration

    Group validation

    Conditional validation

    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.

Causes errors to be generated when unknown parameters exist.

Unknown parameters exist in validated parameter hashref.

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

Replace default messages.

Trim field values. ON by default.

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

Create a new Input::Validator object.

    $validator->clear_errors;

Clear errors.

    $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) });

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

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

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

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

Set a custom error.

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

Return a hash reference of errors.

    $validator->has_errors;

Check if there are any errors.

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

    $validator->all_values;

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

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

Viacheslav Tykhanovskyi, "vti@cpan.org".

In alphabetical order:

Alex Voronov

Anatoliy Lapitskiy

Glen Hinkle

Marcus Ramberg

Naoya Ito

Yaroslav Korshak

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.

2011-10-27 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.