| 1 | package Sledge::Plugin::FormValidator::Lazy; |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | |
|---|
| 6 | use vars qw/$CONSTRAINTS/ ; |
|---|
| 7 | |
|---|
| 8 | use Data::FormValidator; |
|---|
| 9 | use UNIVERSAL::require; |
|---|
| 10 | |
|---|
| 11 | our $VERSION = '0.01_01'; |
|---|
| 12 | |
|---|
| 13 | sub import { |
|---|
| 14 | my $pkg = caller(0); |
|---|
| 15 | |
|---|
| 16 | no strict 'refs'; ## no critic |
|---|
| 17 | *{"$pkg\::create_validator"} = sub { |
|---|
| 18 | my $self = shift; |
|---|
| 19 | $self->{dfv_stash} = {}; |
|---|
| 20 | my $config = $self->create_config->form_validator_lazy; |
|---|
| 21 | my $method_pkg = $config->{method_pkg}; |
|---|
| 22 | $method_pkg->require or die $@; |
|---|
| 23 | $CONSTRAINTS->{_strict} |
|---|
| 24 | = _make_constraints( $method_pkg , $config->{strict} , 'strict' ); |
|---|
| 25 | |
|---|
| 26 | $CONSTRAINTS->{_loose} |
|---|
| 27 | = _make_constraints( $method_pkg , $config->{loose} , 'loose' ); |
|---|
| 28 | |
|---|
| 29 | $CONSTRAINTS->{_regexp} |
|---|
| 30 | = _make_constraint_regexp_map( $method_pkg , $config->{regexp_map} ); |
|---|
| 31 | }; |
|---|
| 32 | |
|---|
| 33 | *{"$pkg\::has_dfv_error"} = \&has_dfv_error; |
|---|
| 34 | *{"$pkg\::dfv_push_invalid"} = \&dfv_push_invalid; |
|---|
| 35 | *{"$pkg\::form"} = \&form; |
|---|
| 36 | *{"$pkg\::dfv_stash"} = \&dfv_stash; |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | sub dfv_stash : lvalue { |
|---|
| 42 | my $self = shift; |
|---|
| 43 | $self->{dfv_stash} ||= {}; |
|---|
| 44 | $self->{dfv_stash}; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | #{{{ has_dfv_error |
|---|
| 48 | sub has_dfv_error { |
|---|
| 49 | my $self = shift; |
|---|
| 50 | |
|---|
| 51 | if ( $self->form->has_invalid or $self->form->has_missing or exists $self->dfv_stash->{custom_invalid} ) { |
|---|
| 52 | return 1; |
|---|
| 53 | } |
|---|
| 54 | else { |
|---|
| 55 | return 0; |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | #}}} |
|---|
| 59 | #{{{ dfv_push_invalid |
|---|
| 60 | sub dfv_push_invalid { |
|---|
| 61 | my $self = shift; |
|---|
| 62 | my $key = shift; |
|---|
| 63 | $self->dfv_stash->{custom_invalid}{ $key } = 1; |
|---|
| 64 | |
|---|
| 65 | } |
|---|
| 66 | #}}} |
|---|
| 67 | |
|---|
| 68 | sub form { |
|---|
| 69 | my $self = shift; |
|---|
| 70 | my $profile = shift; |
|---|
| 71 | |
|---|
| 72 | # FIXME use better implement of singleton if you can. |
|---|
| 73 | unless ( defined $CONSTRAINTS ) { |
|---|
| 74 | $self->create_validator(); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | if ( $profile ) { |
|---|
| 78 | my $conf = $CONSTRAINTS ; |
|---|
| 79 | my $custom_parameters = undef; |
|---|
| 80 | |
|---|
| 81 | if ( !$profile->{constraints} ) { |
|---|
| 82 | my %constraints = %{ $CONSTRAINTS->{_strict} }; |
|---|
| 83 | $profile->{constraints} = \%constraints; |
|---|
| 84 | |
|---|
| 85 | if ( $profile->{constraints_loose} ) { |
|---|
| 86 | for my $key ( @{ $profile->{constraints_loose} } ) { |
|---|
| 87 | $profile->{constraints}{ $key } = $CONSTRAINTS->{_loose}{ $key }; |
|---|
| 88 | } |
|---|
| 89 | delete $profile->{constraints_loose}; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | } |
|---|
| 93 | if( !$profile->{constraint_regexp_map} ) { |
|---|
| 94 | $profile->{constraint_regexp_map} = $CONSTRAINTS->{_regexp} ; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | if( $profile->{custom_parameters} ) { |
|---|
| 98 | $custom_parameters = $profile->{custom_parameters} ; |
|---|
| 99 | delete $profile->{custom_parameters} ; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | $self->{form} = |
|---|
| 104 | Data::FormValidator->check( $custom_parameters || $self->r , $profile ); |
|---|
| 105 | $self->dfv_stash->{valid} = $self->{form}{valid} ; |
|---|
| 106 | $self->dfv_stash->{v} = $self->dfv_stash->{valid}; |
|---|
| 107 | for my $key ( $self->{form}->invalid ) { |
|---|
| 108 | $self->dfv_stash->{invalid}{ $key } = 1; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | for my $key ( $self->{form}->missing ) { |
|---|
| 112 | $self->dfv_stash->{missing}{ $key } = 1; |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | return $self->{form}; |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | } |
|---|
| 120 | #{{{ _make_constraint_regexp_map |
|---|
| 121 | sub _make_constraint_regexp_map { |
|---|
| 122 | my $pkg = shift; |
|---|
| 123 | my $data = shift; |
|---|
| 124 | my $constraints = {}; |
|---|
| 125 | foreach my $key ( keys %{ $data } ) { |
|---|
| 126 | my $value = $data->{ $key } ; |
|---|
| 127 | if( ref $value eq 'ARRAY' ) { |
|---|
| 128 | my $method = $value->[0]; |
|---|
| 129 | my @args = @{ $value }; |
|---|
| 130 | shift @args; |
|---|
| 131 | my $sub = $pkg . '::' . 'static' . '_' . $method ; |
|---|
| 132 | $constraints->{ qr/$key/ } |
|---|
| 133 | = sub { |
|---|
| 134 | my $item = shift ; |
|---|
| 135 | no strict; |
|---|
| 136 | my $result = $sub->( $item ,@args ); |
|---|
| 137 | return $result; |
|---|
| 138 | } |
|---|
| 139 | ; |
|---|
| 140 | } |
|---|
| 141 | else { |
|---|
| 142 | $constraints->{ qr/$key/ } = qr/$value/; |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | return $constraints; |
|---|
| 147 | } |
|---|
| 148 | #}}} |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | #{{{ _make_constraints |
|---|
| 152 | sub _make_constraints { |
|---|
| 153 | my $pkg = shift; |
|---|
| 154 | my $data = shift; |
|---|
| 155 | my $mode = shift; |
|---|
| 156 | |
|---|
| 157 | my $constraints = {}; |
|---|
| 158 | |
|---|
| 159 | foreach my $key ( keys %{ $data } ) { |
|---|
| 160 | my $value = $data->{ $key }; |
|---|
| 161 | |
|---|
| 162 | if ( $value eq 'method' ) { |
|---|
| 163 | local *glob = $pkg . '::' . $mode . '_' . $key; |
|---|
| 164 | $constraints->{ $key } = \&glob ; |
|---|
| 165 | } |
|---|
| 166 | elsif ( ref $value eq 'ARRAY' ) { |
|---|
| 167 | my $method = $value->[0]; |
|---|
| 168 | my @args = @{ $value }; |
|---|
| 169 | shift @args; |
|---|
| 170 | my $sub = $pkg . '::' . 'static' . '_' . $method ; |
|---|
| 171 | $constraints->{ $key } |
|---|
| 172 | = sub { |
|---|
| 173 | my $item = shift ; |
|---|
| 174 | no strict; |
|---|
| 175 | my $result = $sub->( $item ,@args ); |
|---|
| 176 | return $result; |
|---|
| 177 | } |
|---|
| 178 | ; |
|---|
| 179 | } |
|---|
| 180 | else { |
|---|
| 181 | $constraints->{ $key } = qr/$value/; |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | return $constraints; |
|---|
| 185 | } |
|---|
| 186 | #}}} |
|---|
| 187 | 1; |
|---|
| 188 | |
|---|
| 189 | =head1 NAME |
|---|
| 190 | |
|---|
| 191 | Sledge::Plugin::FormValidator::Lazy - use Data::FormValidator in lazy way |
|---|
| 192 | |
|---|
| 193 | =head1 SYNOPSYS |
|---|
| 194 | |
|---|
| 195 | package Your::Pages; |
|---|
| 196 | |
|---|
| 197 | use Sledge::Plugin::FormValidator::Lazy; |
|---|
| 198 | |
|---|
| 199 | ; |
|---|
| 200 | |
|---|
| 201 | conf.yaml |
|---|
| 202 | |
|---|
| 203 | form_validator_lazy : |
|---|
| 204 | method_pkg : Your::Constraints |
|---|
| 205 | strict : |
|---|
| 206 | '_id$' : '^\d+' |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | package Your::Pages::Root; |
|---|
| 210 | |
|---|
| 211 | sub dispatcher_index { |
|---|
| 212 | my $self = shift; |
|---|
| 213 | |
|---|
| 214 | $self->form({ |
|---|
| 215 | requied => [qw/user_id member_id/], |
|---|
| 216 | optional=> [qw/my_id/], |
|---|
| 217 | }); |
|---|
| 218 | |
|---|
| 219 | if( $self->has_dfv_error ) { |
|---|
| 220 | $self->redirect('/error'); |
|---|
| 221 | return ; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | my $v = $self->form->valid; |
|---|
| 225 | |
|---|
| 226 | my $user_id = $v->{user_id} ; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | =head1 DESCRIPTION |
|---|
| 230 | |
|---|
| 231 | This is still under develop, so be aware . |
|---|
| 232 | |
|---|
| 233 | Implement Data::FormValidator in lazy way like L<Catalyst::Plugin::FormValidator::Lazy> |
|---|
| 234 | |
|---|
| 235 | =head1 METHOD |
|---|
| 236 | |
|---|
| 237 | =head2 import |
|---|
| 238 | |
|---|
| 239 | =head2 has_dfv_error |
|---|
| 240 | |
|---|
| 241 | =head2 form |
|---|
| 242 | |
|---|
| 243 | =head2 dfv_push_invalid |
|---|
| 244 | |
|---|
| 245 | =head2 dfv_stash |
|---|
| 246 | |
|---|
| 247 | missing ,invalid , valid , custom_invalid 情報が格納されます。 |
|---|
| 248 | |
|---|
| 249 | =head1 SEE ALSO |
|---|
| 250 | |
|---|
| 251 | L<Catalyst::Plugin::FormValidator::Lazy> |
|---|
| 252 | |
|---|
| 253 | =head1 AUTHOR |
|---|
| 254 | |
|---|
| 255 | Tomohiro Teranishi |
|---|
| 256 | |
|---|
| 257 | =cut |
|---|