HTML::ValidationRules #kansaipm 14

HTML::ValidationRules #kansaipm 14

23f4d5d797a91b6d17d627b90b5a42d9?s=128

Kentaro Kuribayashi

November 26, 2011
Tweet

Transcript

  1. HTML::ValidationRules id:antipop / @kentaro Kansai.pm #14

  2. Validator Modules are Too Complicated to Understand

  3. • FormValidtor::* • Class::Validotor • Data::Validator • etc.

  4. There’re too many validation modules around.

  5. I can’t learn so many modules.

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

    a subset / basis of general validation rules.
  7. None
  8. Client-side Validation

  9. Just use it.

  10. HTML::ValidationRules Written by id:antipop id:wakabatan

  11. • Extracts validation rules from HTML form • Validate server-side

    queries with it • Common rules between client-side and server-side
  12. <!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>
  13. 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 ] ], # ]
  14. 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);
  15. Use HTML5 Client-Side Form Validation also for Server-Side Validation Rules