Amon2-Plugin-Web-FormValidator-Simple

 view release on metacpan or  search on metacpan

lib/Amon2/Plugin/Web/FormValidator/Simple.pm  view on Meta::CPAN

package Amon2::Plugin::Web::FormValidator::Simple;
use utf8;
use strict;
use warnings;
use 5.008001;
our $VERSION = '0.04';

use FormValidator::Simple;

sub init { my ($class, $c, $conf) = @_; #{{{
    my $s = $c->config->{validator};

    my $plugins = $s && exists $s->{plugins} ? $s->{plugins} : [];
    FormValidator::Simple->import(@$plugins);

    FormValidator::Simple->set_messages($s->{messages})
        if $s && exists $s->{messages};
    FormValidator::Simple->set_option(%{$s->{options}})
        if $s && exists $s->{options};
    FormValidator::Simple->set_message_format($s->{message_format})
        if $s && exists $s->{message_format};
    FormValidator::Simple->set_message_decode_from($s->{message_decode_from})
            if $s && exists $s->{message_decode_from};

    Amon2::Util::add_method($c, 'form', \&form);
    Amon2::Util::add_method($c, 'set_invalid_form', \&set_invalid_form);

    $c->add_trigger(BEFORE_DISPATCH => sub { my $c = shift;
        $c->{validator} = FormValidator::Simple->new;

        return;
    });
} #}}}

sub form { my $c = shift; #{{{
    if ($_[0]) {
        my $form = $_[1] ? [@_] : $_[0];
        $c->{validator}->check($c->req, $form);
    }

    return $c->{validator}->results;
} #}}}

sub set_invalid_form { my $c = shift; #{{{
    $c->{validator}->set_invalid(@_);

    return $c->{validator}->results;
} #}}}

1;
__END__

=encoding utf-8

=head1 NAME

Amon2::Plugin::Web::FormValidator::Simple - Amon2 plugin

=head1 SYNOPSIS

    # MyApp.pm



( run in 0.878 second using v1.01-cache-2.11-cpan-39bf76dae61 )