Data::FormValidator Simplified

1,695 views

Published on

The talk I gave at SF.pm on Data::FormValidator on March 23rd, 2010

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,695
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
















  • Data::FormValidator Simplified

    1. 1. Data::FormValidator Simplified Fred Moyer Red Hot Penguin Consulting LLC http://redhotpenguin.com
    2. 2. The Form update account name, email and password current_email a hidden input with current email
    3. 3. The Request Args A hash of request arguments, or a CGI like object with a param() method akin to CGI::param %req = ( email => ‘fred@slwifi.com’, current_email => ‘foo@slwifi.com’, passwd => ‘foo’, retype => ‘foo’, );
    4. 4. Validation Profile %profile = ( required => [qw( current_email email account )], optional => [qw( password retype )], constraint_methods => { email => [ email(), # built in DFV constraint not_current_user({ # function local to package fields => [ 'email','current_email' ] }), ], # end email password => SL::App::check_password({ # common validation fields => [ 'retype', 'password' ],}), }, );
    5. 5. Custom Validation not_current_user() sub not_current_user { return sub { my $dfv = shift; my $val = $dfv->get_current_constraint_value; my $data = $dfv->get_filtered_data; my $email = $data->{email}; my $current = $data->{current_email}; return $val if ( $email eq $current ); # no change my ($reg) = SL::Model::App->resultset('Reg')->search( { email => $email } ); return if $reg; # oops existing user return $val; } }
    6. 6. Custom Validation SL::App::check_password sub check_password { return sub { my $dfv = shift; my $val = $dfv->get_current_constraint_value; my $data = $dfv->get_filtered_data; my $pass = $data->{password}; my $retype = $data->{retype}; return unless length($pass) > 5; # check length return unless ($pass eq $retype); # check match return $val; } }
    7. 7. Validation Execution Apply the %profile to the %request arguments $results = Data::FormValidator->check( %req, %profile );
    8. 8. Validation Results Data::FormValidator::Results $results = ( { 'profile' => { 'dependencies' => {}, 'defaults_regexp_map' => {}, 'constraint_method_regexp_map' => {}, 'constraint_methods' => { 'css_url' => sub { "DUMMY" }... 'unknown' => {}, 'missing' => { 'active' => 1 }, 'valid' => { 'name' => 'test', 'bug_id' => '2' }, 'invalid' => {} }, 'Data::FormValidator::Results' );
    9. 9. Syntactic Sugar Make the results object into a hash for ease of templating if ( $results->has_missing or $results->has_invalid ) { my $errors = $self->SUPER::_results_to_errors($results); ... sub _results_to_errors { my ( $self, $results ) = @_; my %errors; if ( $results->has_missing ) { %{ $errors{missing} } = map { $_ => 1 } $results->missing; } if ( $results->has_invalid ) { %{ $errors{invalid} } = map { $_ => 1 } $results->invalid; } return %errors; }
    10. 10. Send Errors to Template Hand the errors back to the template for display %tmpl_data = ( errors => $errors, ...); <label for="email" class="tip clear [% IF errors.invalid.email or errors.missing.email %]error[% END %] ">Email address</label>
    11. 11. Pretty Form Errors Apply a css class to highlight errors
    12. 12. Useful Error Messages Give the users a clue about what happened [% IF errors %]<p class="error">Oops, correct the fields and try again.</p>[% END %] [% IF errors.invalid.password %]<p class="error">Passwords didn’t match or too short</p>[% END %]
    13. 13. More Code Samples Some content may be inappropriate or undecipherable
    14. 14. Quick Review Multiple Constraints User Defined Constraints Email, etc. Validation Included Easy Integration with CGI->params or Hash Request Args Easy Template Integration
    15. 15. Questions?
    16. 16. Thank You http://search.cpan.org/dist/Data-FormValidator

    ×