Hello monks, this is requests for comments, so if you are interested in this topic, please leave any.

Preface

Dancer2 is a lightweight framework that provides us with rich routing and serialization features out of the box. The rest is left to the developer, including working with the database, models and data in general.

When working with data, especially received from a user, you want to be sure of their consistency. In other words, I don't want to get a number where the username should be and the word in the age field (like age => twenty).

Perl is a language with very powerful regular expressions and many hackers use them. However, this approach has the following disadvantages:

Instead of writing if ($age !~ /^\d+$/) {…} else {…} and so for each field received from the user, I want to use a construction like (age => 'integer').

And here you should pay attention to special modules that simplify the validation process.

Modules

I've found some useful modules on cpan:

They all have their pros and cons, but none of them met all my criteria.

Criteria

1. Control flow the field to the validator, and not vice versa

This means that the code used fields as first class entities:

{ email => qw(required, email), age => qw(integer, min:18), }

And not validators:

{ required => qw(email), email => qw(email), integer => qw(age), min => { filed => ‘age’, num => 18 }, }

2. Required is validator too

Very often you can see:

{ required => … , validators => { … } }

Requred is the same validator and it is not clear to me why to extract it into a separate entity.

3. Has a set of built-in validators

Not all modules have built-in validators and you need to implement them yourself.

4. Easily extendable with new modules or custom code

This feature is provided by most modules.

5. It has a minimal set of dependencies and does not require the mandatory use of DBIc or Moose

Because we have a Dancer2 project and maybe we don't use DBIc models at all.

6. Multilingual support

Didn't find it anywhere.

7. Tightly integrated with Dancer2 and provides a set of handy dsl words and html templates tokens

I do not want to constantly get and transfer data to the template like errors or old input.

Dancer2::Plugin::FormValidator

Based on the above criteria, I decided to write my plugin specifically for Dancer2. Since this is my first big OS project, I ask respected monks to share their opinion on what I got as a result.

use Dancer2; use Dancer2::Plugin::FormValidator; package RegisterForm { use Moo; with 'Dancer2::Plugin::FormValidator::Role::Profile'; sub profile { return { username => [ qw(required alpha_num_ascii length_min:4 + length_max:32) ], email => [ qw(required email length_max:127) ], password => [ qw(required length_max:40) ], password_cnf => [ qw(required same:password) ], confirm => [ qw(required accepted) ], }; } } post '/form' => sub { if (validate profile => RegisterForm->new) { my $valid_hash_ref = validated; save_user_input($valid_hash_ref); redirect '/success_page'; } redirect '/form'; };

Plugin can be found on Metacpan => Dancer2::Plugin::FormValidator.
Github repo => dancer2-plugin-formvalidator.

I am think about the following questions:


In reply to RFC: Creating Dancer2 validation plugin. by AlexP

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.