@@ -98,21 +98,42 @@ use constant FIELDS => [
9898 ' recitation' , ' comment' , ' permission' , ' password'
9999];
100100
101- # Note that only the editable fields need a type (i.e. all but user_id),
102- # and only the text fields need a size.
101+ # Note that only the editable fields need a type (i.e. all but user_id).
102+ # The fields of type text or number may also include optional attributes for the HTML input.
103+ # Any field may also contain a perlValidate method that will be called to validate user input. If provided, it should be
104+ # a subroutine that takes the parameter value as its only argument, and returns a translatable error string if the
105+ # parameter value is not valid for the field, and 0 otherwise.
103106use constant FIELD_PROPERTIES => {
104107 user_id => { name => x(' Login Name' ) },
105- first_name => { name => x(' First Name' ), type => ' text' , size => 10 },
106- last_name => { name => x(' Last Name' ), type => ' text' , size => 10 },
107- email_address => { name => x(' Email Address' ), type => ' text' , size => 20 },
108- student_id => { name => x(' Student ID' ), type => ' text' , size => 11 },
109- status => { name => x(' Enrollment Status' ), type => ' status' },
110- accommodation_time_factor => { name => x(' Accommodation Time Factor' ), type => ' text' , size => 5 },
111- section => { name => x(' Section' ), type => ' text' , size => 3 },
112- recitation => { name => x(' Recitation' ), type => ' text' , size => 3 },
113- comment => { name => x(' Comment' ), type => ' text' , size => 20 },
114- permission => { name => x(' Permission Level' ), type => ' permission' },
115- password => { name => x(' Password' ), type => ' password' },
108+ first_name => { name => x(' First Name' ), type => ' text' , attributes => { size => 10 } },
109+ last_name => { name => x(' Last Name' ), type => ' text' , attributes => { size => 10 } },
110+ email_address => { name => x(' Email Address' ), type => ' text' , attributes => { size => 20 } },
111+ student_id => { name => x(' Student ID' ), type => ' text' , attributes => { size => 11 } },
112+ status => { name => x(' Enrollment Status' ), type => ' status' },
113+ accommodation_time_factor => {
114+ name => x(' Accommodation Time Factor' ),
115+ type => ' number' ,
116+ attributes => {
117+ size => 5,
118+ min => 1,
119+ step => ' any' ,
120+ title => ' Enter a decimal number that is greater than or equal to 1.'
121+ },
122+ perlValidate => sub {
123+ my $value = shift ;
124+ return $value !~ / ^(\d +(\.\d *)?|\.\d +)$ / || $value <= 0
125+ ? (x(
126+ ' Accomodation time factor for [_1] unchanged. '
127+ . ' A value was given that is not a decimal number or is not greater than or equal to 1.'
128+ ))[0]
129+ : 0;
130+ }
131+ },
132+ section => { name => x(' Section' ), type => ' text' , attributes => { size => 3 } },
133+ recitation => { name => x(' Recitation' ), type => ' text' , attributes => { size => 3 } },
134+ comment => { name => x(' Comment' ), type => ' text' , attributes => { size => 20 } },
135+ permission => { name => x(' Permission Level' ), type => ' permission' },
136+ password => { name => x(' Password' ), type => ' password' },
116137};
117138
118139sub pre_header_initialize ($c ) {
@@ -519,7 +540,14 @@ sub save_edit_handler ($c) {
519540
520541 for my $field ($User -> NONKEYFIELDS()) {
521542 my $newValue = $c -> param(" user.$userID .$field " );
522- $User -> $field ($newValue ) if defined $newValue ;
543+ next unless defined $newValue ;
544+ if (ref (FIELD_PROPERTIES()-> {$field }{perlValidate }) eq ' CODE'
545+ && (my $error = FIELD_PROPERTIES()-> {$field }{perlValidate }-> ($newValue )))
546+ {
547+ $c -> addbadmessage($c -> maketext($error , $userID ));
548+ next ;
549+ }
550+ $User -> $field ($newValue );
523551 }
524552 $db -> putUser($User );
525553
0 commit comments