Upgrade to Pro — share decks privately, control downloads, hide ads and more …

HTML::ValidationRules #kansaipm 14

HTML::ValidationRules #kansaipm 14

Kentaro Kuribayashi

November 26, 2011
Tweet

More Decks by Kentaro Kuribayashi

Other Decks in Technology

Transcript

  1. HTML5 Client Side Form Validation Spec. can be regarded as

    a subset / basis of general validation rules.
  2. • Extracts validation rules from HTML form • Validate server-side

    queries with it • Common rules between client-side and server-side
  3. <!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="range"" name="range" min="20" max="80" /> <input type="submit" value="submit" /> </form> </body> </html>
  4. 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 ], 'NOT_BLANK' ], # email => [ HTML_EMAIL => [ HTML_MAXLENGTH => 255 ], 'NOT_BLANK' ], # number => [ HTML_NUMBER => [ HTML_MIN => 200 ], [ HTML_MAX => 800 ] ], # textarea => [ [ HTML_MAXLENGTH => 1000 ], 'NOT_BLANK' ], # range => [ HTML_RANGE => [ HTML_MIN => 20 ], [ HTML_MAX => 80 ] ], # ]
  5. use FormValidator::Simple qw(HTML); my $query = CGI->new; my $result =

    FormValidator::Simple- >check($query => $rules); # or FormValidator::Lite use FormValidator::Lite; FormValidator::Lite->load_constraints('HTML'); my $query = CGI->new; my $validator = FormValidator::Lite- >new($query); my $result = $validator->check(@$rules);