Skip to content

Commit

Permalink
renamed "validate" helper to "do_validation"
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor Turskyi committed Oct 6, 2011
1 parent a833a53 commit 379badf
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions lib/Mojolicious/Plugin/ValidateTiny.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,32 @@ use Mojo::Base 'Mojolicious::Plugin';

use Validate::Tiny;

our $VERSION = '0.01';
our $VERSION = '0.02';

sub register {
my ( $self, $app, $conf ) = @_;
$conf ||= {};

$app->helper(
validate => sub {
do_validation => sub {
my ( $self, $rules, $params ) = @_;
$params ||= $self->req->params->to_hash;

my $result = Validate::Tiny->new( $params, $rules );
if ( $result->success ) {
$self->app->log->debug('ValidateTiny: Successful');
return $result->data;
} else {
$self->stash( validator_errors => $result->error );
$self->app->log->debug('ValidateTiny: Failed: ' . join( ' ,' , keys %{$result->error} ));
$self->stash( validate_tiny_errors => $result->error );
return;
}
} );

$app->helper(
validator_has_errors => sub {
my $self = shift;
my $errors = $self->stash('validator_errors');
my $errors = $self->stash('validate_tiny_errors');

return 0 if !$errors || !keys %$errors;
return 1;
Expand All @@ -34,7 +37,9 @@ sub register {
$app->helper(
validator_error => sub {
my ( $self, $name ) = @_;
my $errors = $self->stash('validator_errors');
my $errors = $self->stash('validate_tiny_errors');

return $errors unless defined $name;

if ( $errors && defined $errors->{$name} ) {
return $errors->{$name};
Expand All @@ -58,18 +63,27 @@ Mojolicious::Plugin::ValidateTiny - Mojolicious Plugin
sub action {
my $self = shift;
# Validate $self->param()
my $validate_rules = {};
if ( my $params = $self->validate($validate_rules) ) {
if ( my $params = $self->do_validation($validate_rules) ) {
# all $params are validated and filters are applyed
... do you action ...
# Validate custom data
my $rules = {...};
my $data = {...};
if ( my $data = $self->do_validation($rules, $data) ) {
} else {
my $errors_hash = $self->validator_error();
}
} else {
$self->render(status => '403', text => 'FORBIDDEN');
}
}
__DATA__
@@ user.html.ep
Expand Down Expand Up @@ -119,6 +133,9 @@ Check if there are any errors.
=head2 validator_error
my $errors_hash = $self->validator_error();
my $username_error = $self->validator_error('username');
<%= validator_error 'username' %>
Render the appropriate error.
Expand Down

0 comments on commit 379badf

Please sign in to comment.