| 1 | package Dancer::Plugin::DataFormValidator; | ||
| 2 | { | ||
| 3 | $Dancer::Plugin::DataFormValidator::VERSION = '0.002'; | ||
| 4 | } | ||
| 5 | # ABSTRACT: Easy access to Data::FormValidator from within Dancer applications | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use Data::Dump qw{pp}; | ||
| 10 | use Dancer ':syntax'; | ||
| 11 | use Dancer::Plugin; | ||
| 12 | use Data::FormValidator; | ||
| 13 | |||
| 14 | |||
| 15 | register dfv => sub { | ||
| 16 | my ($profile, $input) = @_; | ||
| 17 | |||
| 18 | debug 'Checking for explicit input'; | ||
| 19 | unless ($input) { | ||
| 20 | debug 'Getting params implicitly'; | ||
| 21 | $input = params; | ||
| 22 | } | ||
| 23 | |||
| 24 | debug "input is " . pp $input; | ||
| 25 | |||
| 26 | debug 'Checking for explicit profile'; | ||
| 27 | if (ref $profile) { | ||
| 28 | debug 'Checking using explicit profile'; | ||
| 29 | return Data::FormValidator->check ($input, $profile); | ||
| 30 | } | ||
| 31 | elsif ($profile) { | ||
| 32 | debug 'Looking for cached Data::FormValidator object'; | ||
| 33 | my $dfv = config->{validator}->{_dfv}; | ||
| 34 | unless ($dfv) { | ||
| 35 | debug 'Creating Data;:FormValidator object for the first time'; | ||
| 36 | config->{validator}->{_dfv} = $dfv = Data::FormValidator->new (join '/', setting ('appdir'), plugin_setting->{profile_file}); | ||
| 37 | } | ||
| 38 | debug 'Creating Data;:FormValidator object for the first time'; | ||
| 39 | debug 'Checking using named profile'; | ||
| 40 | return $dfv->check ($input, $profile); | ||
| 41 | } else { | ||
| 42 | error 'No profile specified for doing validation'; | ||
| 43 | } | ||
| 44 | |||
| 45 | return 0; | ||
| 46 | }; | ||
| 47 | |||
| 48 | register_plugin; | ||
| 49 | |||
| 50 | 1; | ||
| 51 | |||
| 52 | __END__ |