Dancer2-Plugin-FormValidator

 view release on metacpan or  search on metacpan

lib/Dancer2/Plugin/FormValidator.pm  view on Meta::CPAN

    ### You can use HashRef returned from validate.

    if (my $valid_hash_ref = validate profile => RegisterForm->new) {
        # Success, data is valid.
    }


    ### Or more declarative approach with validated keyword.

    if (validate profile => RegisterForm->new) {
        # Success, data is valid.
        my $valid_hash_ref = validated;

        # Do some operations...
    }
    else {
        # Error, data is invalid.
        my $errors = errors; # errors keyword returns error messages.

        # Redirect or show errors...
    }

=head3 validated

    validated(): HashRef|undef

No arguments.
Returns valid input I<HashRef> if validate succeed.
I<Undef> value will be returned after first call within one validation process.

    my $valid_hash_ref = validated;

=head3 errors

    errors(): HashRef

No arguments.
Returns I<HashRef[ArrayRef]> if validation failed.

    my $errors_hash_multi = errors;

=head1 Validators

=head3 accepted

    accepted(): Bool

Validates that field B<exists> and one of the listed: (yes on 1).

    field => [ qw(accepted) ]

=head3 alpha

    alpha(Str $encoding = 'a'): Bool

Validate that string only contain of alphabetic symbols.
By default encoding is ascii, i.e B</^[[:alpha:]]+$/a>.

    field => [ qw(alpha) ]

To set encoding to unicode you need to pass 'u' argument:

    field => [ qw(alpha:u) ]

Then the validation rule will be B</^[[:alpha:]]+$/>.

=head3 alpha_num

    alpha_num(Str $encoding = 'a'): Bool

Validate that string only contain of alphabetic symbols, underscore and numbers 0-9.
By default encoding is ascii, i.e. B</^\w+$/a>.

    field => [ qw(alpha_num) ]

To set encoding to unicode you need to pass 'u' argument:

    field => [ qw(alpha_num:u) ]

Rule will be B</^\w+$/>.

=head3 boolean

    boolean(): Bool

Validate that field is 0 or 1 scalar value.

    field => [ qw(boolean) ]

=head3 email

    email(): Bool

Validate that field is valid email(B<rfc822>).

    field => [ qw(email) ]

=head3 email_dns

    email_dns(): Bool

Validate that field is valid email(B<rfc822>) and dns exists.

    field => [ qw(email_dns) ]

=head3 enum

    enum(Array @values): Bool

Validate that field is one of listed values.

    field => [ qw(enum:value1,value2) ]

=head3 integer

    integer(): Bool

Validate that field is integer.

    field => [ qw(integer) ]

=head3 length_max

    length_max(Int $num): Bool

Validate that string length <= num.

    field => [ qw(length_max:32) ]

=head3 length_min

    length_min(Int $num): Bool

Validate that string length >= num.

    field => [ qw(length_max:4) ]



( run in 2.286 seconds using v1.01-cache-2.11-cpan-63c85eba8c4 )