Mojolicious-Plugin-Validate-Tiny

 view release on metacpan or  search on metacpan

README.pod  view on Meta::CPAN


=head1 NAME

Mojolicious::Plugin::Validate::Tiny - Lightweight validator for Mojolicious

=head1 SEE

This plugin is a copy of L<https://github.com/koorchik/Mojolicious-Plugin-ValidateTiny>, with the intent to have a plugin that it's maintained

=head1 SYNOPSIS

    # Mojolicious
    $self->plugin('Validate::Tiny');
    
    # Mojolicious::Lite
    plugin 'Validate::Tiny';
    
    sub action { 
        my $self = shift;
        my $validate_rules = [
            # All of these are required
            [qw/name email pass pass2/] => is_required(),

            # pass2 must be equal to pass
            pass2 => is_equal('pass'),

            # custom sub validates an email address
            email => sub {
                my ( $value, $params ) = @_;
                Email::Valid->address($value) ? undef : 'Invalid email';
            }
        ];
        return unless $self->do_validation($validate_rules);
        
        ... Do something ...
    }
        
        
    sub action2 {
        my $self = shift;

        my $validate_rules = { 
            checks  => [...],
            fields  => [...],
            filters => [...]
        };
        if ( my $filtered_params =  $self->do_validation($validate_rules) ) {
            # all $params are validated and filters are applyed
            ... do your action ...

         
        } else {
            my $errors     = $self->validator_error;             # hash with errors
            my $pass_error = $self->validator_error('password'); # password error text
            my $any_error  = $self->validator_any_error;         # any error text
            
            $self->render( status => '403', text => $any_error );  
        }
        
    }
    
    __DATA__
  
    @@ user.html.ep
    %= if (validator_has_errors) {
        <div class="error">Please, correct the errors below.</div>
    % }
    %= form_for 'user' => begin
        <label for="username">Username</label><br />
        <%= input_tag 'username' %><br />
        <%= validator_error 'username' %><br />
  
        <%= submit_button %>
    % end

  
=head1 DESCRIPTION

L<Mojolicious::Plugin::Validate::Tiny> is a L<Validate::Tiny> support for L<Mojolicious>.

=head1 OPTIONS

=head2 C<explicit> (default 0)

If "explicit" is true then for every field must be provided check rule

=head2 C<autofields> (default 1)

If "autofields" then validator will automatically create fields list based on passed checks.
So, you can pass: 
    [
        user => is_required(),
        pass => is_required(),
    ]

instead of 

    {
        fields => ['user', 'pass'],
        checks => [
            user => is_required(),
            pass => is_required(),
        ]
    }

=head2 C<exclude> (default [])

Is an arrayref with a list of fields that will be never checked.



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