Changeset 7713

Show
Ignore:
Timestamp:
03/09/08 19:57:50 (5 years ago)
Author:
tomyhero
Message:

lang/perl/Data::FormValidate? : Bug fix. multiple error was not worked well.

Location:
lang/perl/Data-FormValidateEm/trunk
Files:
7 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Data-FormValidateEm/trunk/conf/sample.conf

    r7691 r7713  
    1414                args : 
    1515                    max : 50 
    16                     min : 2 
     16                    min : 10 
    1717            - String::string  
    1818            - +OreOrePlugin::OreOre::foo 
  • lang/perl/Data-FormValidateEm/trunk/lib/Data/FormValidateEm.pm

    r7691 r7713  
    2626    $self->{constraints}{regex_map} = $self->_make_constraints('regex_map'); 
    2727 
    28     #warn $self->get_message( 'title', 'String::length' ); 
    2928    return $self; 
    3029} 
  • lang/perl/Data-FormValidateEm/trunk/lib/Data/FormValidateEm/Plugin/String.pm

    r7691 r7713  
    77    my $text = shift; 
    88    my $args = shift; 
     9 
    910    die 'you must set max argument' unless exists $args->{max}; 
    1011    die 'you must set min argument' unless exists $args->{min}; 
  • lang/perl/Data-FormValidateEm/trunk/lib/Data/FormValidateEm/Results.pm

    r7691 r7713  
    77use Carp; 
    88use 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); 
    1012 
    1113sub new { 
     
    1820    my $args        = shift; 
    1921 
    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 ); 
    2225    return $self; 
    2326} 
     
    3033    my $field    = shift; 
    3134    my $label    = shift; 
    32     my $type     = shift ; 
     35    my $type     = shift; 
     36 
    3337    #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} || {}; 
    3842    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}; 
    4448        $message =~ s/$regexp/$value/g; 
    4549    } 
     
    5761    my $args        = shift; 
    5862 
    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; 
    6265    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 
    7376    # Remove all empty fields 
    7477    for my $field ( keys %valid ) { 
     
    116119    for my $field ( keys %required ) { 
    117120        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; 
    127133                delete $valid{$field}; 
    128134            } 
    129135        } 
    130136        else { 
    131             push @missings, $field ; 
     137            push @missings, $field; 
    132138            delete $valid{$field}; 
    133              
     139 
    134140        } 
    135141    } 
    136142 
    137143    for my $field ( keys %required ) { 
    138  
     144        my $is_invalid = 0; 
    139145        # if no valud do not need to check it 
    140146        next if any(@missings) eq $field; 
    141147 
    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}; 
    143152 
    144153        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}; 
    147156            $type->{$field} = 'loose'; 
    148157        } 
    149158        else { 
    150             $validators = $constraints->{strict}{$field} ; 
     159            $validators = $constraints->{strict}{$field}; 
    151160            $type->{$field} = 'strict'; 
    152161        } 
    153162 
    154163        # regexp_map try~ 
    155         if( !defined $validators ) { 
     164        if ( !defined $validators ) { 
    156165            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}; 
    159168                    $type->{$field} = 'regex_map'; 
    160169                } 
     
    166175                if ( ref $valid{$field} eq 'ARRAY' ) { 
    167176                    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! 
    170181                        } 
    171182                        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++; 
    176193                            last; 
    177194                        } 
    178195                    } 
    179196 
    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                    { 
    181200                        $valid{$field} = $valid{$field}[0]; 
    182201                        last; 
     
    186205                else { 
    187206                    if ( $validators->{$name}{method}->( $valid{$field} ) ) { 
     207 
    188208                        # 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                        { 
    190211                            my $value = $valid{$field}; 
    191212                            $valid{$field} = []; 
    192                             push @{ $valid{$field} } , $value; 
     213                            push @{ $valid{$field} }, $value; 
    193214                        } 
    194215 
     
    196217                    else { 
    197218                        $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++; 
    201226                    } 
    202227                } 
     
    207232        } 
    208233 
    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; 
    224251    $self->{error_messages} = $error_messages; 
    225252 
    226253    $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} 
    234258 
    235259# takes string or array ref as input 
  • lang/perl/Data-FormValidateEm/trunk/t/error-messages.t

    r7691 r7713  
    3131=== normal ok test 
    3232--- input yaml 
    33 title   : a  
     33title   : foo 
    3434subject : a 
    3535--- profile yaml 
     
    4444{ 
    4545subject => { 'String::length' => 1 }, 
    46 title   => { 'String::length' => 1 } 
     46title   => { 'String::length' => 1 , '+OreOrePlugin::OreOre::foo' => 1 } 
    4747} 
    4848--- unknown eval 
     
    5050--- error_messages yaml 
    5151subject :  
    52     - 2文字以上100文字以下で題名をご記入ください。 
     52    - 2文字以上100文字以下で<span class="label">題名</span>をご記入ください。 
    5353title : 
    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  
    44 
    55sub dfv_foo { 
    6     1; 
     6    return shift eq 'foo' ? 0 : 1 ; 
    77} 
    88 
  • lang/perl/Data-FormValidateEm/trunk/t/profile-defaults.t

    r7251 r7713  
    3535    - title 
    3636defaults : 
    37     title : aaaa 
     37    title : aaaaaaaaaaa 
    3838--- valid yaml 
    39 title :  aaaa 
     39title :  aaaaaaaaaaa 
    4040--- missing eval 
    4141[] 
     
    4646=== not overwrite 
    4747--- input yaml 
    48 title: bbb 
     48title: bbbbbbbbbbbbb 
    4949--- profile yaml 
    5050required : 
    5151    - title 
    5252defaults : 
    53     title : aaaa 
     53    title : bbbbbb 
    5454--- valid yaml 
    55 title :  bbb 
     55title :  bbbbbbbbbbbbb 
    5656--- missing eval 
    5757[]