PrePAN

Sign in to PrePAN

HTML::ValidationRules Extract Validation Rules from HTML5 Form Elements

Good

Synopsis

# form.html
# <!doctype html>
# <html>
#   <head>
#     <meta charset="UTF-8">
#     <title>HTML5::ValidationRules</title>
#   </head>
#   <body>
#     <form method="post" action="/post">
#       <input type="text" name="text" pattern="[a-zA-Z0-9]+" maxlength="255" />
#       <input type="url" name="url" maxlength="255" required />
#       <input type="email" name="email" maxlength="255" required="required" />
#       <input type="number" name="number" min="200" max="800" />
#       <textarea name="textarea" maxlength="1000" required></textarea>
#       <input type="submit" value="submit" />
#     </form>
#   </body>
# </html>

# Extract validation rules from HTML
use HTML::ValidationRules;

my $parser = HTML::ValidationRules->new;
my $rules  = $parser->load_rules(file => 'form.html');

# Rules will be extracted as follows:
# [
#     text     => [ [ HTML_PATTERN => '[a-zA-Z0-9]+' ], [ HTML_MAXLENGTH => 255 ] ],
#     url      => [ HTML_URL    => [ HTML_MAXLENGTH => 255 ], 'HTML_REQUIRED'     ],
#     email    => [ HTML_EMAIL  => [ HTML_MAXLENGTH => 255 ], 'HTML_REQUIRED'     ],
#     number   => [ HTML_NUMBER => [ HTML_MIN => 200 ], [ HTML_MAX => 800 ]       ],
#     textarea => [ [ HTML_MAXLENGTH => 1000 ], 'HTML_REQUIRED'                   ],
# ]

# Then use them
use FormValidator::Simple qw(HTML);

my $query  = CGI->new;
my $result = FormValidator::Simple->check($query => $rules);

Description

Even if we validate user inputs on client-side according to client-side form validation spec. defined in HTML5, we still need do same thing on server-side. It results that there're redundant validation rules around. How about regarding client-side form validation as some common format for describing validation rules both for client-side and server-side?

This distribution consists of 2 parts modules below:

  • HTML parser to extract validation rules from given HTML file/string.
  • Plugin for existing validation module (now for FormValidator::Simple because I usually use it).

This module has been kinda proof-of-concept thing yet.

  • Too few supports of attributes defined in HTML5 now.
  • I'm planning to write plugin except FV::S, for example, FormValidator::Lite).

Comments

Not sure if you were aware, but Validation::Class was just released, maybe you could write a plugin for that - http://goo.gl/TZ0Wq and http://goo.gl/4zVCl
It looks a bit hard to adapt this module to Validation::Class. I have to make up some easy-to-treat structure of validation rules.
Actually you're right, upon further investigation I see where you're going. The simplest solution for you would to simply extend whatever validation framework you're using to generate HTML with specific attribute your client-side validator can understand. FYI, Validation::Class::Plugin::FormFields does this already.
Hmm, I don't focus on `generating HTML' via some module.What I want to do is commonarizing validation rules between clien/server side. I found HTML5 spec. provides common way for that, so I was hit by the idea, using form attributes as rules also at server-side.
When we define validation rules at server-side, we have to make up some own format. There're many independent ones around. I don't want to learn how I can write rules for each validation modules. HTML5 spec. is so common for web developers that we can do that at 1 learning.
I guess it just depends on your requirements. The HTML5 spec is not powerful enough (alone) to secure any of my webapps which means I'll eventually end up writing custom rules that aren't covered by the HTML5 spec, ... and if that the case ... I might-have just not used it in the first place.

What if your using ajax to send data, ... now your interface (while it -may still work) .. is weird and awkward because the form.html file that the rules are based on may not fit the context. Etc.

But if it works for you ...
I totally agree with you on that HTML5 spec is not enough for all of requirements of webapps. For example, to assure username is unique, we need some custom validation logic.

Originally, this module is just a proposal so far. It's not clear even for me this module works well ;) I'll try it here, PrePAN, first.

Please sign up to post a review.