Mojolicious::Plugin::Validator - Plugin for MojoX::Validator
# Mojolicious $self->plugin('validator'); # Mojolicious::Lite plugin 'validator' => { messages => { REQUIRED => 'This field is required', LENGTH_CONSTRAINT_FAILED => 'Too big' } }; sub action { my $self = shift; my $validator = $self->create_validator; $validator->field('username')->required(1)->length(3, 20); return unless $self->validate($validator); # Create a user for example ... } 1; __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::Validator is a plugin for MojoX::Validator that simplifies parameters validation.
# Mojolicious::Lite plugin 'validator' => { messages => { REQUIRED => 'This field is required', LENGTH_CONSTRAINT_FAILED => 'Too big' } };
Replace default errors.
my $validator = $self->create_validator; $validator->field('username')->required(1)->length(3, 20);
Create MojoX::Validator.
$self->create_validator('will-be_decamelized'); $self->create_validator('Custom::Class');
Create a validator from a class derived from MojoX::Validator. This way preconfigured validators can be used.
$self->validate($validator);
Validate parameters with provided validator and automatically set errors.
%= if (validator_has_errors) { <div class="error">Please, correct the errors below.</div> % }
Check if there are any errors.
<%= validator_error 'username' %>
Render the appropriate error.
%= if (validator_has_unknown_params) { <div class="error">Unspecified parameters were detected.</div> % }
Returns true if unspecified parameters were passed
Mojolicious::Plugin::Validator inherits all methods from Mojolicious::Plugin and implements the following new ones.
register
$plugin->register;
Register helpers in Mojolicious application.
MojoX::Validator, Mojolicious.
To install MojoX::Validator, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MojoX::Validator
CPAN shell
perl -MCPAN -e shell install MojoX::Validator
For more information on module installation, please visit the detailed CPAN module installation guide.