Diff for /loncom/interface/lonhelper.pm between versions 1.37 and 1.38

version 1.37, 2003/06/12 13:52:06 version 1.38, 2003/06/17 14:21:22
Line 779  some setting accidentally. Line 779  some setting accidentally.
   
 Again, see the course initialization helper for examples.  Again, see the course initialization helper for examples.
   
   B<validator tag>
   
   Some elements that accepts user input can contain a "validator" tag that,
   when surrounded by "sub { my $helper = shift; my $state = shift; my $element = shift; my $val = shift " 
   and "}", where "$val" is the value the user entered, will form a subroutine 
   that when called will verify whether the given input is valid or not. If it 
   is valid, the routine will return a false value. If invalid, the routine 
   will return an error message to be displayed for the user.
   
   Consult the documentation for each element to see whether it supports this 
   tag.
   
 B<getValue method>  B<getValue method>
   
 If the element stores the name of the variable in a 'variable' member, which  If the element stores the name of the variable in a 'variable' member, which
Line 790  this method. Line 802  this method.
 BEGIN {  BEGIN {
     &Apache::lonhelper::register('Apache::lonhelper::element',      &Apache::lonhelper::register('Apache::lonhelper::element',
                                  ('nextstate', 'finalcode',                                   ('nextstate', 'finalcode',
                                   'defaultvalue'));                                    'defaultvalue', 'validator'));
 }  }
   
 # Because we use the param hash, this is often a sufficent  # Because we use the param hash, this is often a sufficent
Line 855  sub start_defaultvalue { Line 867  sub start_defaultvalue {
   
 sub end_defaultvalue { return ''; }  sub end_defaultvalue { return ''; }
   
   sub start_validator {
       my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   
       if ($target ne 'helper') {
           return '';
       }
       
       $paramHash->{VALIDATOR} = &Apache::lonxml::get_all_text('/validator',
                                                                $parser);
       $paramHash->{VALIDATOR} = 'sub { my $helper = shift; my $state = shift; my $element = shift; my $val = shift;' .
           $paramHash->{VALIDATOR} . '}';
       return '';
   }
   
   sub end_validator { return ''; }
   
 sub preprocess {  sub preprocess {
     return 1;      return 1;
 }  }
Line 2266  also pass through 'maxlength' and 'size' Line 2294  also pass through 'maxlength' and 'size'
   
 string honors the defaultvalue tag, if given.  string honors the defaultvalue tag, if given.
   
   string honors the validation function, if given.
   
 =cut  =cut
   
 no strict;  no strict;
Line 2311  sub end_string { Line 2341  sub end_string {
   
 sub render {  sub render {
     my $self = shift;      my $self = shift;
     my $result = '<input type="string" name="' . $self->{'variable'} . '.forminput"';      my $result = '';
   
       if (defined $self->{ERROR_MSG}) {
           $result .= '<br /><font color="#FF0000">' . $self->{ERROR_MSG} . '</font><br /><br />';
       }
   
       $result .= '<input type="string" name="' . $self->{'variable'} . '.forminput"';
   
     if (defined($self->{'size'})) {      if (defined($self->{'size'})) {
         $result .= ' size="' . $self->{'size'} . '"';          $result .= ' size="' . $self->{'size'} . '"';
Line 2335  sub render { Line 2371  sub render {
 # If a NEXTSTATE was given, switch to it  # If a NEXTSTATE was given, switch to it
 sub postprocess {  sub postprocess {
     my $self = shift;      my $self = shift;
     if (defined($self->{NEXTSTATE})) {  
         $helper->changeState($self->{NEXTSTATE});      if (defined($self->{VALIDATOR})) {
    my $validator = eval($self->{VALIDATOR});
    die 'Died during evaluation of evaulation code; Perl said: ' . $@ if $@;
    my $invalid = &$validator($helper, $state, $self, $self->getValue());
    if ($invalid) {
       $self->{ERROR_MSG} = $invalid;
       return 0;
    }
       }
   
       if (defined($self->{'nextstate'})) {
           $helper->changeState($self->{'nextstate'});
     }      }
   
     return 1;      return 1;
Line 2650  sub render { Line 2697  sub render {
     # FIXME: Unify my designators with the standard ones      # FIXME: Unify my designators with the standard ones
     my %dateTypeHash = ('open_date' => "Opening Date",      my %dateTypeHash = ('open_date' => "Opening Date",
                         'due_date' => "Due Date",                          'due_date' => "Due Date",
                         'answer_date' => "Answer Date");                          'answer_date' => "Answer Date",
    'tries' => 'Number of Tries'
    );
     my %parmTypeHash = ('open_date' => "0_opendate",      my %parmTypeHash = ('open_date' => "0_opendate",
                         'due_date' => "0_duedate",                          'due_date' => "0_duedate",
                         'answer_date' => "0_answerdate");                          'answer_date' => "0_answerdate",
    'tries' => '0_maxtries' );
           
     my $affectedResourceId = "";      my $affectedResourceId = "";
     my $parm_name = $parmTypeHash{$vars->{ACTION_TYPE}};      my $parm_name = $parmTypeHash{$vars->{ACTION_TYPE}};
Line 2699  sub render { Line 2749  sub render {
     $result .= '<p>Confirm that this information is correct, then click &quot;Finish Wizard&quot; to complete setting the parameter.<ul>';      $result .= '<p>Confirm that this information is correct, then click &quot;Finish Wizard&quot; to complete setting the parameter.<ul>';
           
     # Print the type of manipulation:      # Print the type of manipulation:
     $result .= '<li>Setting the <b>' . $dateTypeHash{$vars->{ACTION_TYPE}}      $result .= '<li>Setting the <b>' . $dateTypeHash{$vars->{ACTION_TYPE}} . '</b>';
                . "</b></li>\n";      if ($vars->{ACTION_TYPE} eq 'tries') {
    $result .= ' to <b>' . $vars->{TRIES} . '</b>';
       }
       $result .= "</li>\n";
     if ($vars->{ACTION_TYPE} eq 'due_date' ||       if ($vars->{ACTION_TYPE} eq 'due_date' || 
         $vars->{ACTION_TYPE} eq 'answer_date') {          $vars->{ACTION_TYPE} eq 'answer_date') {
         # for due dates, we default to "date end" type entries          # for due dates, we default to "date end" type entries
Line 2717  sub render { Line 2770  sub render {
             "value='" . $vars->{PARM_DATE} . "' />\n";              "value='" . $vars->{PARM_DATE} . "' />\n";
         $result .= "<input type='hidden' name='pres_type' " .          $result .= "<input type='hidden' name='pres_type' " .
             "value='date_start' />\n";              "value='date_start' />\n";
     }       } elsif ($vars->{ACTION_TYPE} eq 'tries') {
    $result .= "<input type='hidden' name='pres_value' " .
       "value='" . $vars->{TRIES} . "' />\n";
       }
   
     $result .= $resourceString;      $result .= $resourceString;
           
Line 2747  sub render { Line 2803  sub render {
     }      }
   
     # Print value      # Print value
     $result .= "<li>to <b>" . ctime($vars->{PARM_DATE}) . "</b> (" .      if ($vars->{ACTION_TYPE} ne 'tries') {
         Apache::lonnavmaps::timeToHumanString($vars->{PARM_DATE})    $result .= "<li>to <b>" . ctime($vars->{PARM_DATE}) . "</b> (" .
         . ")</li>\n";      Apache::lonnavmaps::timeToHumanString($vars->{PARM_DATE}) 
       . ")</li>\n";
       }
    
     # print pres_marker      # print pres_marker
     $result .= "\n<input type='hidden' name='pres_marker'" .      $result .= "\n<input type='hidden' name='pres_marker'" .
         " value='$affectedResourceId&$parm_name&$level' />\n";          " value='$affectedResourceId&$parm_name&$level' />\n";

Removed from v.1.37  
changed lines
  Added in v.1.38


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>