Changeset 7713
- Timestamp:
- 03/09/08 19:57:50 (5 years ago)
- Location:
- lang/perl/Data-FormValidateEm/trunk
- Files:
-
- 7 modified
-
conf/sample.conf (modified) (1 diff)
-
lib/Data/FormValidateEm.pm (modified) (1 diff)
-
lib/Data/FormValidateEm/Plugin/String.pm (modified) (1 diff)
-
lib/Data/FormValidateEm/Results.pm (modified) (9 diffs)
-
t/error-messages.t (modified) (3 diffs)
-
t/lib/OreOrePlugin/OreOre.pm (modified) (1 diff)
-
t/profile-defaults.t (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/Data-FormValidateEm/trunk/conf/sample.conf
r7691 r7713 14 14 args : 15 15 max : 50 16 min : 216 min : 10 17 17 - String::string 18 18 - +OreOrePlugin::OreOre::foo -
lang/perl/Data-FormValidateEm/trunk/lib/Data/FormValidateEm.pm
r7691 r7713 26 26 $self->{constraints}{regex_map} = $self->_make_constraints('regex_map'); 27 27 28 #warn $self->get_message( 'title', 'String::length' );29 28 return $self; 30 29 } -
lang/perl/Data-FormValidateEm/trunk/lib/Data/FormValidateEm/Plugin/String.pm
r7691 r7713 7 7 my $text = shift; 8 8 my $args = shift; 9 9 10 die 'you must set max argument' unless exists $args->{max}; 10 11 die 'you must set min argument' unless exists $args->{min}; -
lang/perl/Data-FormValidateEm/trunk/lib/Data/FormValidateEm/Results.pm
r7691 r7713 7 7 use Carp; 8 8 use Data::Dumper; 9 __PACKAGE__->mk_accessors(qw/valid invalid unknown missing has_error has_invalid has_missing error_messages/); 9 __PACKAGE__->mk_accessors( 10 qw/valid invalid unknown missing has_error has_invalid has_missing error_messages/ 11 ); 10 12 11 13 sub new { … … 18 20 my $args = shift; 19 21 20 my $self = bless {}, $class; 21 $self->_process( $constraints, $profile, $data , $messages , $labels, $args ); 22 my $self = bless {}, $class; 23 $self->_process( $constraints, $profile, $data, $messages, $labels, 24 $args ); 22 25 return $self; 23 26 } … … 30 33 my $field = shift; 31 34 my $label = shift; 32 my $type = shift ; 35 my $type = shift; 36 33 37 #FIXME 34 my $lang = 'ja';35 36 my $message = $messages->{ $label }{ $lang };37 my $args = $arg->{$type}{$field}{$label} || {};38 my $lang = 'ja'; 39 40 my $message = $messages->{$label}{$lang}; 41 my $args = $arg->{$type}{$field}{$label} || {}; 38 42 my $__field__ = $labels->{$lang}{$field} || $field; 39 $message =~ s/__field__/ $__field__/g;40 41 foreach my $key ( keys %{ $args} ) {42 my $regexp = '\$_\[' . $key . '\]' ;43 my $value = $args->{$key};43 $message =~ s/__field__/<span class="label">$__field__<\/span>/g; 44 45 foreach my $key ( keys %{$args} ) { 46 my $regexp = '\$_\[' . $key . '\]'; 47 my $value = $args->{$key}; 44 48 $message =~ s/$regexp/$value/g; 45 49 } … … 57 61 my $args = shift; 58 62 59 60 my %data = $self->_get_input_as_hash($data); 61 my %valid = %data; 63 my %data = $self->_get_input_as_hash($data); 64 my %valid = %data; 62 65 my $error_messages = {}; 63 my @missings = ();64 my @unknown = ();65 my %invalid = ();66 67 68 if( defined $profile->{defaults} ) {69 foreach my $field (keys %{ $profile->{defaults} } ) {70 $valid{$field} ||= $profile->{defaults}{ $field } ;71 }72 } 66 my @missings = (); 67 my @unknown = (); 68 my %invalid = (); 69 70 if ( defined $profile->{defaults} ) { 71 foreach my $field ( keys %{ $profile->{defaults} } ) { 72 $valid{$field} ||= $profile->{defaults}{$field}; 73 } 74 } 75 73 76 # Remove all empty fields 74 77 for my $field ( keys %valid ) { … … 116 119 for my $field ( keys %required ) { 117 120 push @missings, $field unless exists $valid{$field}; 118 delete $valid{$field} unless exists $valid{$field}; 119 } 120 121 # Check must_be_plural 122 # TODO : even user set too much prameters which generate missing error which look strange.... 123 foreach my $field ( keys %{$profile->{must_be_plural}} ) { 124 if( ref $valid{$field} eq 'ARRAY' ) { 125 unless( scalar @{$valid{$field}} == $profile->{must_be_plural}{$field} ) { 126 push @missings, $field ; 121 delete $valid{$field} unless exists $valid{$field}; 122 } 123 124 # Check must_be_plural 125 # TODO : even user set too much prameters which generate missing error which look strange.... 126 foreach my $field ( keys %{ $profile->{must_be_plural} } ) { 127 if ( ref $valid{$field} eq 'ARRAY' ) { 128 unless ( 129 scalar @{ $valid{$field} } 130 == $profile->{must_be_plural}{$field} ) 131 { 132 push @missings, $field; 127 133 delete $valid{$field}; 128 134 } 129 135 } 130 136 else { 131 push @missings, $field ;137 push @missings, $field; 132 138 delete $valid{$field}; 133 139 134 140 } 135 141 } 136 142 137 143 for my $field ( keys %required ) { 138 144 my $is_invalid = 0; 139 145 # if no valud do not need to check it 140 146 next if any(@missings) eq $field; 141 147 142 my $validators = any( @{ $profile->{use_loose} } ) eq $field ? $constraints->{loose}{$field} : $constraints->{strict}{$field}; 148 my $validators 149 = any( @{ $profile->{use_loose} } ) eq $field 150 ? $constraints->{loose}{$field} 151 : $constraints->{strict}{$field}; 143 152 144 153 my $type = {}; 145 if ( any( @{ $profile->{use_loose} } ) eq $field) {146 $validators = $constraints->{loose}{$field} ;154 if ( any( @{ $profile->{use_loose} } ) eq $field ) { 155 $validators = $constraints->{loose}{$field}; 147 156 $type->{$field} = 'loose'; 148 157 } 149 158 else { 150 $validators = $constraints->{strict}{$field} ;159 $validators = $constraints->{strict}{$field}; 151 160 $type->{$field} = 'strict'; 152 161 } 153 162 154 163 # regexp_map try~ 155 if ( !defined $validators ) {164 if ( !defined $validators ) { 156 165 foreach my $regexp ( keys %{ $constraints->{regex_map} } ) { 157 if ( $field =~ qr/$regexp/ ) {158 $validators = $constraints->{regex_map}{ $regexp };166 if ( $field =~ qr/$regexp/ ) { 167 $validators = $constraints->{regex_map}{$regexp}; 159 168 $type->{$field} = 'regex_map'; 160 169 } … … 166 175 if ( ref $valid{$field} eq 'ARRAY' ) { 167 176 for my $v ( @{ $valid{$field} } ) { 168 if ( $validators->{$name}{method}->( $valid{$field} ) ) { 169 # ok! 177 if ($validators->{$name}{method}->( $valid{$field} ) ) 178 { 179 180 # ok! 170 181 } 171 182 else { 172 $invalid{$field}{ $validators->{$name}{label} } = 1; 173 $error_messages->{$field} = [] unless exists $error_messages->{$field}; 174 push @{ $error_messages->{$field} } ,$self->_get_message( $args , $labels ,$messages, $field , $validators->{$name}{label} , $type->{ $field } ); 175 delete $valid{$field}; 183 $invalid{$field}{ $validators->{$name}{label} } 184 = 1; 185 $error_messages->{$field} = [] 186 unless exists $error_messages->{$field}; 187 push @{ $error_messages->{$field} }, 188 $self->_get_message( $args, $labels, 189 $messages, $field, 190 $validators->{$name}{label}, 191 $type->{$field} ); 192 $is_invalid++; 176 193 last; 177 194 } 178 195 } 179 196 180 if( ! defined $profile->{must_be_plural}{$field} && none( @{ $profile->{may_be_plural} } ) eq $field ) { 197 if ( !defined $profile->{must_be_plural}{$field} 198 && none( @{ $profile->{may_be_plural} } ) eq $field ) 199 { 181 200 $valid{$field} = $valid{$field}[0]; 182 201 last; … … 186 205 else { 187 206 if ( $validators->{$name}{method}->( $valid{$field} ) ) { 207 188 208 # return alwasy array ref when may_be_plural is seted. 189 if( any( @{ $profile->{may_be_plural} } ) eq $field ) { 209 if ( any( @{ $profile->{may_be_plural} } ) eq $field ) 210 { 190 211 my $value = $valid{$field}; 191 212 $valid{$field} = []; 192 push @{ $valid{$field} } , $value;213 push @{ $valid{$field} }, $value; 193 214 } 194 215 … … 196 217 else { 197 218 $invalid{$field}{ $validators->{$name}{label} } = 1; 198 $error_messages->{$field} = [] unless exists $error_messages->{$field}; 199 push @{ $error_messages->{$field} } ,$self->_get_message( $args , $labels, $messages, $field , $validators->{$name}{label} , $type->{ $field } ); 200 delete $valid{$field}; 219 $error_messages->{$field} = [] 220 unless exists $error_messages->{$field}; 221 push @{ $error_messages->{$field} }, 222 $self->_get_message( $args, $labels, $messages, 223 $field, $validators->{$name}{label}, 224 $type->{$field} ); 225 $is_invalid++; 201 226 } 202 227 } … … 207 232 } 208 233 209 } 210 211 # FIXME : Just for now. 212 # invalid , missing もそうだけど、 __field__ とかの置き換えするとかで、フィールド名も表示できたほがいいよな。 213 if( scalar @missings ) { 214 for my $missing ( @missings ) { 215 $error_messages->{$missing} = [] unless exists $error_messages->{$missing}; 216 push @{ $error_messages->{ $missing } } , 'missing.'; 217 } 218 } 219 220 $self->{valid} = \%valid; 221 $self->{invalid} = \%invalid; 222 $self->{missing} = \@missings; 223 $self->{unknown} = \@unknown; 234 delete $valid{$field} if $is_invalid; 235 } 236 237 # FIXME : Just for now. 238 # invalid , missing もそうだけど、 __field__ とかの置き換えするとかで、フィールド名も表示できたほがいいよな。 239 if ( scalar @missings ) { 240 for my $missing (@missings) { 241 $error_messages->{$missing} = [] 242 unless exists $error_messages->{$missing}; 243 push @{ $error_messages->{$missing} }, 'missing.'; 244 } 245 } 246 247 $self->{valid} = \%valid; 248 $self->{invalid} = \%invalid; 249 $self->{missing} = \@missings; 250 $self->{unknown} = \@unknown; 224 251 $self->{error_messages} = $error_messages; 225 252 226 253 $self->{has_missing} = scalar @missings ? 1 : 0; 227 $self->{has_invalid} = keys %invalid ? 1 : 0; 228 $self->{has_error} = $self->{has_missing} || $self->{has_invalid} ? 1 : 0; 229 230 231 } 232 233 254 $self->{has_invalid} = keys %invalid ? 1 : 0; 255 $self->{has_error} = $self->{has_missing} || $self->{has_invalid} ? 1 : 0; 256 257 } 234 258 235 259 # takes string or array ref as input -
lang/perl/Data-FormValidateEm/trunk/t/error-messages.t
r7691 r7713 31 31 === normal ok test 32 32 --- input yaml 33 title : a33 title : foo 34 34 subject : a 35 35 --- profile yaml … … 44 44 { 45 45 subject => { 'String::length' => 1 }, 46 title => { 'String::length' => 1 }46 title => { 'String::length' => 1 , '+OreOrePlugin::OreOre::foo' => 1 } 47 47 } 48 48 --- unknown eval … … 50 50 --- error_messages yaml 51 51 subject : 52 - 2文字以上100文字以下で 題名をご記入ください。52 - 2文字以上100文字以下で<span class="label">題名</span>をご記入ください。 53 53 title : 54 - 2文字以上50文字以下でtitleをご記入ください。 54 - ore ore 55 - 10文字以上50文字以下で<span class="label">title</span>をご記入ください。 -
lang/perl/Data-FormValidateEm/trunk/t/lib/OreOrePlugin/OreOre.pm
r7282 r7713 4 4 5 5 sub dfv_foo { 6 1;6 return shift eq 'foo' ? 0 : 1 ; 7 7 } 8 8 -
lang/perl/Data-FormValidateEm/trunk/t/profile-defaults.t
r7251 r7713 35 35 - title 36 36 defaults : 37 title : aaaa 37 title : aaaaaaaaaaa 38 38 --- valid yaml 39 title : aaaa 39 title : aaaaaaaaaaa 40 40 --- missing eval 41 41 [] … … 46 46 === not overwrite 47 47 --- input yaml 48 title: bbb 48 title: bbbbbbbbbbbbb 49 49 --- profile yaml 50 50 required : 51 51 - title 52 52 defaults : 53 title : aaaa53 title : bbbbbb 54 54 --- valid yaml 55 title : bbb 55 title : bbbbbbbbbbbbb 56 56 --- missing eval 57 57 []
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)