Skip to content

Commit

Permalink
improved POD
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor Turskyi committed Jan 10, 2012
1 parent ca73fc4 commit 5ea7b92
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 70 deletions.
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Revision history for Mojolicious::Plugin::ValidateTiny

0.07
First version, released on an unsuspecting world.
5 changes: 5 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Changes
MANIFEST
Makefile.PL
README.pod
lib/Mojolicious/Plugin/ValidateTiny.pm
5 changes: 4 additions & 1 deletion Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ WriteMakefile(
'Mojolicious' => 2.42,
'Validate::Tiny' => 0.08
},
test => { TESTS => 't/*.t' },
PREREQ_PRINT => 1,
test => { TESTS => 't/*.t' },
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
clean => { FILES => 'Mojolicious-Plugin-ValidateTiny-*' },

);
129 changes: 107 additions & 22 deletions README.pod
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

=head1 NAME

Mojolicious::Plugin::ValidateTiny - Mojolicious Plugin
Expand All @@ -10,20 +11,50 @@ Mojolicious::Plugin::ValidateTiny - Mojolicious Plugin
# Mojolicious::Lite
plugin 'ValidateTiny';

sub action {
sub action {
my $self = shift;

my $validate_rules = {};
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 ...
}

if ( my $params = $self->validate($validate_rules) ) {

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 you action ...
... do your action ...


} else {
$self->render(status => '403', text => 'FORBIDDEN');
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
Expand All @@ -41,44 +72,98 @@ Mojolicious::Plugin::ValidateTiny - Mojolicious Plugin

=head1 DESCRIPTION

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

=head1 OPTIONS

=head2 C<explicit> (default 0)

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

L<Mojolicious::Plugin::ValidateTiny> inherits all methods from
L<Mojolicious::Plugin> and implements the following new ones.
=head2 C<autofields> (default 1)

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

$plugin->register;
instead of

Register plugin in L<Mojolicious> application.
{
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.

For example, if you use "Mojolicious::Plugin::CSRFProtect" then you should add "csrftoken" to exclude list.

=head1 HELPERS

=head2 validate
=head2 C<do_validation>

Validates parameters with provided rules and automatically set errors.

$self->validate($validate_rules);
$VALIDATE_RULES - Validate::Tiny rules in next form

Validate parameters with provided validator and automatically set errors.
{
checks => $CHECKS, # Required
fields => [], # Optional (will check all GET+POST parameters)
filters => [], # Optional
}

You can pass only "checks" arrayref to "do_validation".
In this case validator will take all GET+POST parameters as "fields"

returns false if validation failed
returns true if validation succeded

$self->do_validation($VALIDATE_RULES)
$self->do_validation($CHECKS);


=head2 C<validator_has_errors>

Check if there are any errors.

=head2 validator_has_errors
if ($self->validator_has_errors) {
$self->render_text( $self->validator_any_error );
}

%= if (validator_has_errors) {
<div class="error">Please, correct the errors below.</div>
% }

Check if there are any errors.
=head2 C<validator_error>

=head2 validator_error
Returns the appropriate error.

my $errors_hash = $self->validator_error();
my $username_error = $self->validator_error('username');

<%= validator_error 'username' %>

=head2 C<validator_any_error>

Returns any of the existing errors. This method is usefull if you want return only one error.

=head1 AUTHOR

Viktor Turskyi <koorchik@cpan.org>

=head1 BUGS

Render the appropriate error.
Please report any bugs or feature requests to Github L<https://github.com/koorchik/Mojolicious-Plugin-ValidateTiny>

=head1 SEE ALSO

L<Validate::Tiny>, L<Mojolicious>, L<Mojolicious::Plugin::Validator>
L<Validate::Tiny>, L<Mojolicious>, L<Mojolicious::Plugin::Validator>

=cut
Loading

0 comments on commit 5ea7b92

Please sign in to comment.