NAME

Validator::Custom::Ext::Mojolicious - Validator for Mojolicious

VERSION

Version 0.0103

SYNOPSIS

package YourApp;
use base 'Mojolicious';

use Validator::Custom::Ext::Mojolicious;

__PACKAGE__->attr(validator => sub {
    
    return Validator::Custom::Ext::Mojolicious->new(
        validator_class  => 'Validator::Custom::HTMLForm',
        validation_rules => {
            'create#default' => [
                title => [
                    [{length => [0, 255]}, 'Title is too long']
                ],
                brash => [
                    ['not_blank', 'Select brach'],
                    [{'in_array' => [qw/bash cpp c-sharp/]}, 'Brash is invalid']
                ],
                content => [
                    [ 'not_blank',           "Input content"],
                    [ {length => [0, 4096]}, "Content is too long"]
                ]
            ],
            'example#welcome' => [
                # ...
            ]
        }
    );
    
});

package YourApp::Create;
use base 'Mojolicious::Controller';

sub default { 
    my $self = shift;
    
    # Validate
    my $vresult = $self->app->validator->validate($self);
    
    unless ($vresult->is_valid) {
       # Someting 
    }
}

Attributes

validator_class

$v->validator_class('Validator::Custom::HTMLForm');

This class must be Validator::Custom subclass like Validator::Custom::HTMLForm.

You can also set object, not class

my $vc = Validator::Custom::HTMLForm->new(error_stock => 0);
$v->validator_class($vc);

validation_rules

You can set validation rules correspond to controller and action pair. Constoller and action must be join '#'.

$v->validation_rules({
    'create#default' => [
        title => [
            [{length => [0, 255]}, 'title is too long']
        ],
        brash => [
            ['not_blank', 'brash must exists'],
            [{'in_array' => [qw/bash cpp/]}, 
             'brash select is invalid']
        ],
        content => [
            [ 'not_blank',         'Content must be exists'],
            [ {length => [0, 4096]}, 'Conten is too long']
        ]
    ],
    'action#controller' =>[
            # ...
    ]
});

Validation rule is explained Validator::Custom documentation.

Methods

Validator::Custom::Ext::Mojolicious inherits all methods from Object::Simple::Base and implements the following new ones.

validate

Validate received data

my $vresult = $v->validate($c);

This method receive Mojolicious::Controller object. and validate request parameters. and return validation rusult. This result is Validator::Custom::Result object.

Author

Yuki Kimoto, <kimoto.yuki at gmail.com>

Copyright & License

Copyright 2009 Yuki Kimoto, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.