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
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 ...
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.