NAME

Validator::Custom::Ext::Mojolicious - Mojolicious validator

VERSION

Version 0.0302

STABILITY

This module is not stable. APIs will be changed.

SYNOPSIS

use Mojolicious::Lite;

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

my $validator = Validator::Custom::Ext::Mojolicious->new(
    validator  => 'Validator::Custom::HTMLForm',
    rules => {
        create => [
            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"]
            ]
        ],
        index => [
            # ...
        ]
    }
);

post '/create' => sub {
    my $self = shift;
    
    # Validate
    my $vresult = $validator->validate($self);
    
    unless ($vresult->is_valid) {
       # Someting 
    }        

} => 'create'; # Route name

ATTRIBUTES

validator

$validator->validator('Validator::Custom::HTMLForm');

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

You can also set object, not class

$validator->validator(Validator::Custom::HTMLForm->new(error_stock => 0));

rules

You can set validation rules correspond to route name.

$validator->rules({
    'create' => [
        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']
        ]
    ],
    'index' =>[
            # ...
    ]
});

Validation rule is explained in Validator::Custom.

METHODS

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

validate

Validate received data

my $vresult = $validator->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.