NAME

Dancer2::Plugin::FormValidator - validate incoming request in declarative way.

VERSION

version 0.01

SYNOPSIS

use Dancer2::Plugin::FormValidator;

post '/form' => sub {
    if (my $valid_hash_ref = validate_form 'form') {
        save_user_input($valid_hash_ref);
        redirect '/success_page';
    }

    redirect '/form';
};

DISCLAIMER

This is not stable version!

Please dont rely on it. Interfaces would be changed if future, except of dsl keywords signatures.

If you like it - add it to your bookmarks. I intend to complete the development by the summer 2022.

Have any ideas? Find this project on github (repo ref is at the bottom).

DESCRIPTION

This is micro-framework that provides validation in your Dancer2 application. It consists of dsl's keywords and a set of agreements. It is build around Data::FormValidator.

Uses two approaches: declarative and verbose with more control.

Validator

First, you need to create class which will implements at least one main role: Dancer2::Plugin::FormValidator::Role::HasProfile.

This role requires profile method which should return a HashRef Data::FormValidator accepts:

package App::App::Http::Validators::RegisterForm {
    use Moo;
    use Data::FormValidator::Constraints qw(:closures);

    with 'Dancer2::Plugin::FormValidator::Role::HasProfile';

    sub profile {
        required => [qw(name email)],
        constraint_methods => {
            email => email,
        },
    };
}

Declarative approach

Then you need to set an form => validator association in config:

set plugins => {
       FormValidator => {
           session => {
               namespace => '_form_validator' # This is required field
           },
           forms   => {
               register_form => 'App::App::Http::Validators::RegisterForm',
           },
       },
   };

Now you can validate POST parameters in your controller:

use Dancer2::Plugin::FormValidator;

 post '/register' => sub {
     if (my $valid_hash_ref = validate_form 'register_form') {
         if (login($valid_hash_ref)) {
             redirect '/success_page';
         }
     }

     redirect '/register';
 };

In you template you have access to $errors - this is hash with parameters names as keys and error messages as values like:

{
    name  => '<span>Name is missing.</span>',
    email => '<span>Email is invalid.</span>'
}

CONFIGURATION

...
plugins:
    FormValidator:
        session:
            namespace: '_form_validator'           # this is required
        messages:
            missing: '<span>%s is missing.</span>' # default is '%s is missing.'
            invalid: '<span>%s is invalid.</span>' # default is '%s is invalid.'
            ucfirst: 1                             # this is default
        forms:
            login_form: 'App::Http::Validators::LoginForm'
            support_form: 'App::Http::Validators::SupportForm'
            ...
...

DSL KEYWORDS

validate HashRef:$input => Object:$validator

validate_form String:$form

TODO

Configuration details: list all fields and describe them.
Document Result object.
Document all Dsl.
Document all Verbose approach.
Document all Roles and HashRef structures.
Template test $errors.

BUGS AND LIMITATIONS

If you find one, please let me know.

SOURCE CODE REPOSITORY

https://github.com/AlexP007/dancer2-plugin-formvalidator.

AUTHOR

Alexander Panteleev <alexpan at cpan dot org>.

LICENSE AND COPYRIGHT

This software is copyright (c) 2022 by Alexander Panteleev. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.