Mojolicious::Plugin::ValidateTiny - Lightweight validator for Mojolicious
# Mojolicious $self->plugin('ValidateTiny'); # Mojolicious::Lite plugin 'ValidateTiny'; 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
Mojolicious::Plugin::ValidateTiny is a Validate::Tiny support for Mojolicious.
explicit
If "explicit" is true then for every field must be provided check rule
autofields
If "autofields" then validator will automatically create fields list based on checks passed to validator. So, you can pass: [ user => is_required(), pass => is_required(), ]
instead of
{ fields => ['user', 'pass'], checks => [ user => is_required(), pass => is_required(), ] }
exclude
Is an arrayref with a list of fields (names or regexps) that will not be affected by explicit option.
For example, if you use "Mojolicious::Plugin::CSRFProtect" then you should add "csrftoken" to exclude list.
do_validation
Validates parameters with provided rules and automatically set errors.
$VALIDATE_RULES - Validate::Tiny rules in next form
{ 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);
validator_has_errors
Check if there are any 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> % }
validator_error
Returns the appropriate error.
my $errors_hash = $self->validator_error(); my $username_error = $self->validator_error('username'); <%= validator_error 'username' %>
validator_error_string
Returns a string with all errors (an empty string in case of no errors). Helper maps directly to Validate::Tiny::error_string method ( see "error_string" in Validate::Tiny )
my $error_str = $self->validator_error_string(); <%= validator_error_string %>
validator_any_error
Returns any of the existing errors. This method is usefull if you want return only one error.
Viktor Turskyi <koorchik@cpan.org>
Please report any bugs or feature requests to Github https://github.com/koorchik/Mojolicious-Plugin-ValidateTiny
Validate::Tiny, Mojolicious, Mojolicious::Plugin::Validator
To install Mojolicious::Plugin::ValidateTiny, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::ValidateTiny
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::ValidateTiny
For more information on module installation, please visit the detailed CPAN module installation guide.