NAME

Mojolicious::Plugin::Validator - Plugin for MojoX::Validator

SYNOPSIS

# 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

DESCRIPTION

Mojolicious::Plugin::Validator is a plugin for MojoX::Validator that simplifies parameters validation.

Options

messages
# Mojolicious::Lite
plugin 'validator' => {
    messages => {
        REQUIRED                 => 'This field is required',
        LENGTH_CONSTRAINT_FAILED => 'Too big'
    }
};

Replace default errors.

Helpers

create_validator
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.

validate
$self->validate($validator);

Validate parameters with provided validator and automatically set errors.

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

Check if there are any errors.

validator_error
<%= validator_error 'username' %>

Render the appropriate error.

validator_has_unknown_params
% if (validator_has_unknown_params) {
    <div class="error">Unspecified parameters were detected.</div>
% }

Returns true if unspecified parameters were passed

METHODS

Mojolicious::Plugin::Validator inherits all methods from Mojolicious::Plugin and implements the following new ones.

register

$plugin->register;

Register helpers in Mojolicious application.

SEE ALSO

MojoX::Validator, Mojolicious.