[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cgiapp] better msg handling in FormValidator ?


--- Michael Peters <suppressed> wrote:

> James.Q.L wrote:
> 

[snip]

> > but it does give you the real flexiablity of setting up customized message.
> > 
> > one question though, if a field (say username) required at least lenght of 6 and contain only
> > number and alphabet. how do you show different error based on user input for this single
> filed?
> 
> I do this in the custom messages sub. If you provide multiple constraints for
> the same field you can give each one a name. Then when you call invalid() on the
> D::FV::Results object passing in the name of the invalid field you get an array
> ref containing the names of the validation constraints that failed. Smolder does
> this and then uses it to set the error flags.
> 

i implemented something similar to yours with one difference: set err_FILED_NAME TT param with the
real error message regardless it is missing or invalid. so that i only need to use [%
err_FiledName %] in template like:

[% IF err_username %]
<tr><td></td><td>[% err_username %]</td></tr>
[% END %]

in base.pm i have the following:

__PACKAGE__->add_callback('tt_pre_process', sub {
                              my ($self, $file, $vars) = @_;
                              $vars->{remote_user} = $ENV{REMOTE_USER};
                              if ( my $dfv_tt_params = $self->dfv_tt_params ) {
                                  while (my ($k,$v) = each %$dfv_tt_params) {
                                      $vars->{ $k } = $v;
                                  }
                              }
                              return;
                          }
);

sub dfv_tt_params {
    my $self = shift;
    # loop through invalid
    # find constraint name for each field.
    # access the real message with $self->config_param('FIELD_NAME.err_CONSTRAINT_NAME'),

    # skip if check_rm is not called.
    # probably no need of dfv in this case.
    my $r;
    eval { $r = $self->dfv_results };
    return if $@;

    my %tt_param;
    if ( $r->has_invalid ) {
        my $invalid = $r->invalid;
        for my $field ( keys %$invalid ) {
            my $constraint_name = shift @{ $invalid->{$field} };
            $tt_param{ "err_$field" } =
                     $self->config_param("$field.err_$constraint_name");
        }
    }

    if ($r->has_missing) {
        my $missing = $r->missing;
        for my $field (@$missing) {
            $tt_param{ "err_$field" } = 'Required field cannot be left blank';
        }
    }

    return \%tt_param;
}

then i have message.conf that has all error messages for all situation.

[username]
err_length_between=must be between 6 and 30 characters long.
err_invalid_characters=only letters (a-z)\, numbers (0-9)\, and periods (.) are allowed.

the end result is exactly what i am after. 

last question:

in a scalar context, dfv's invalid method returns an hash reference which contains the invalid
fields as keys, and references to arrays of failed constraints as values.

for my observation, the array reference contains only one value - the failed constraint name. i
see that in the Control.pm from smolder, you have 

                my $names = $dfv->invalid($failed);
                foreach my $name (@$names) {
                    next if ( ref $name );    # skip regexes
                    $msgs{"invalid_$name"} = 1;
                }

do you have an example for this? 

thanks!


> -- 
> Michael Peters
> Developer
> Plus Three, LP
> 

James.


      ____________________________________________________________________________________
Park yourself in front of a world of choices in alternative vehicles. Visit the Yahoo! Auto Green Center.
http://autos.yahoo.com/green_center/ 

---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed


Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.