updated for advanced form processing

This commit is contained in:
JT Smith 2003-05-03 20:43:05 +00:00
parent 7d106fb66e
commit b58c87f1a9
3 changed files with 673 additions and 35 deletions

View file

@ -99,6 +99,11 @@ sub _fixTags {
return $value;
}
#-------------------------------------------------------------------
sub _javascriptFile {
return '<script language="JavaScript" src="'.$session{config}{extrasURL}.'/'.$_[0].'"></script>'."\n";
}
#-------------------------------------------------------------------
=head2 checkbox ( hashRef )
@ -340,7 +345,7 @@ The number of characters wide this form element should be. There should be no re
sub email {
my ($output);
$output = '<script language="javascript" src="'.$session{config}{extrasURL}.'/emailCheck.js"></script>';
$output = _javascriptFile('emailCheck.js');;
$output .= text({
name=>$_[0]->{name},
value=>$_[0]->{value},
@ -364,7 +369,7 @@ The name field for this form element.
=item types
An array reference of field types to be displayed. The field names are the names of the methods from this forms package. Note that not all field types are supported.
An array reference of field types to be displayed. The field names are the names of the methods from this forms package. Note that not all field types are supported. Defaults to all.
=item value
@ -395,7 +400,9 @@ sub fieldType {
# without adult supervision. =) It was done this way because a huge
# if/elsif construct executes much more quickly than a bunch of
# unnecessary database hits.
foreach $type (@$_[0]->{types}) {
my @types = qw(zipcode text textarea HTMLArea url date email phone integer yesNo selectList radioList checkboxList checkbox);
$_[0]->{types} = \@types unless ($_[0]->{types});
foreach $type (@{$_[0]->{types}}) {
if ($type eq "text") {
$hash{text} = WebGUI::International::get(475);
} elsif ($type eq "textarea") {
@ -416,6 +423,14 @@ sub fieldType {
$hash{yesNo} = WebGUI::International::get(483);
} elsif ($type eq "selectList") {
$hash{selectList} = WebGUI::International::get(484);
} elsif ($type eq "radioList") {
$hash{radioList} = WebGUI::International::get(942);
} elsif ($type eq "checkboxList") {
$hash{checkboxList} = WebGUI::International::get(941);
} elsif ($type eq "zipcode") {
$hash{zipcode} = WebGUI::International::get(944);
} elsif ($type eq "checkbox") {
$hash{checkbox} = WebGUI::International::get(943);
}
}
return selectList({
@ -581,12 +596,12 @@ The number of characters wide this form element should be. There should be no re
sub float {
my $value = $_[0]->{value} || 0;
my $size = $_[0]->{size} || 11;
my $output = '<script language="JavaScript" src="'.$session{config}{extrasURL}.'/floatCheck.js"></script>';
my $output = _javascriptFile('inputCheck.js');
$output .= text({
name=>$_[0]->{name},
value=>$value,
size=>$size,
extras=>'onKeyUp="doFloatCheck(this.form.'.$_[0]->{name}.')" '.$_[0]->{extras},
extras=>'onKeyUp="doInputCheck(this.form.'.$_[0]->{name}.',\'0123456789.\')" '.$_[0]->{extras},
maxlength=>$_[0]->{maxlength}
});
return $output;
@ -792,8 +807,7 @@ sub HTMLArea {
} </script>';
$output .= $button;
} else {
$output .= '<script language="Javascript1.2" src="'.$session{config}{extrasURL}
.'/htmlArea/editor.js"></script>'."\n";
$output .= _javascriptFile('htmlArea/editor.js');
$output .= '<script>'."\n";
$output .= '_editor_url = "'.$session{config}{extrasURL}.'/htmlArea/";'."\n";
$output .= '</script>'."\n";
@ -815,18 +829,6 @@ sub HTMLArea {
window.open("'.$session{config}{extrasURL}.'/ie5edit.html","editWindow","width=490,height=400,resizable=1"); }
function setContent(content) { formObj.value = content; } </script>';
$output .= $button;
### UNCOMMENT THIS WHEN MOZILLA 1.3 COMES OUT
# } elsif ($session{user}{richEditor} eq "wendedit" && $browser->gecko && $browser->version >= 1.3) {
# $output .= '<script language="JavaScript">
# var formObj;
## var extrasDir="'.$session{config}{extrasURL}.'";
# function openEditWindow(obj) {
# formObj = obj;
# window.open("'.$session{config}{extrasURL}.'/wendedit.html","editWindow","width=490,height=400,resizable=1"); }
# function setContent(content) {
# formObj.value = content;
# } </script>';
# $output .= $button;
} elsif ($session{user}{richEditor} eq "lastResort") {
$output .= '<script language="JavaScript">
var formObj;
@ -896,12 +898,12 @@ sub integer {
my ($output, $size, $value);
$value = $_[0]->{value} || 0;
$size = $_[0]->{size} || 11;
$output = '<script language="JavaScript" src="'.$session{config}{extrasURL}.'/numberCheck.js"></script>';
$output = _javascriptFile('inputCheck.js');
$output .= text({
name=>$_[0]->{name},
value=>$value,
size=>$size,
extras=>'onKeyUp="doNumCheck(this.form.'.$_[0]->{name}.')" '.$_[0]->{extras},
extras=>'onKeyUp="doInputCheck(this.form.'.$_[0]->{name}.',\'0123456789-\')" '.$_[0]->{extras},
maxlength=>$_[0]->{maxlength}
});
return $output;
@ -1041,15 +1043,16 @@ The number of characters wide this form element should be. There should be no re
=cut
sub phone {
my ($maxLength);
$maxLength = $_[0]->{maxLength} || 30;
return text({
my $output = _javascriptFile('inputCheck.js');
my $maxLength = $_[0]->{maxLength} || 30;
$output .= text({
name=>$_[0]->{name},
maxlength=>$maxLength,
extras=>$_[0]->{extras},
extras=>'onKeyUp="doInputCheck(this.form.'.$_[0]->{name}.',\'0123456789-()+ \')" '.$_[0]->{extras},
value=>$_[0]->{value},
size=>$_[0]->{size}
});
return $output;
}
#-------------------------------------------------------------------
@ -1349,7 +1352,7 @@ The number of characters wide this form element should be. There should be no re
sub textarea {
my ($columns, $value, $rows, $wrap);
$wrap = $_[0]->{virtual} || "virtual";
$wrap = $_[0]->{wrap} || "virtual";
$rows = $_[0]->{rows} || $session{setting}{textAreaRows} || 5;
$columns = $_[0]->{columns} || $session{setting}{textAreaCols} || 50;
$value = _fixSpecialCharacters($_[0]->{value});
@ -1394,11 +1397,8 @@ The number of characters wide this form element should be. There should be no re
=cut
sub url {
my ($output, $maxLength);
$maxLength = $_[0]->{maxlength} || 2048;
$output = '<script language="JavaScript">function addHTTP(element) {
if (!element.value.match(":\/\/") && element.value.match(/\.\w+/))
{ element.value = "http://"+element.value}}</script>';
my $maxLength = $_[0]->{maxlength} || 2048;
my $output = _javascriptFile('addHTTP.js');
$output .= text({
name=>$_[0]->{name},
value=>$_[0]->{value},
@ -1537,8 +1537,16 @@ The number of characters wide this form element should be. There should be no re
=cut
sub zipcode {
#this is here for future expansion
return text($_[0]);
my $output = _javascriptFile('inputCheck.js');
my $maxLength = $_[0]->{maxLength} || 10;
$output .= text({
name=>$_[0]->{name},
maxlength=>$maxLength,
extras=>'onKeyUp="doInputCheck(this.form.'.$_[0]->{name}.',\'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ- \')" '.$_[0]->{extras},
value=>$_[0]->{value},
size=>$_[0]->{size}
});
return $output;
}

630
lib/WebGUI/FormProcessor.pm Normal file
View file

@ -0,0 +1,630 @@
package WebGUI::FormProcessor;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2003 Plain Black LLC.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict;
use WebGUI::DateTime;
use WebGUI::HTML;
=head1 NAME
Package WebGUI::FormProcessor;
=head1 DESCRIPTION
This package helps in the processing of the form variables that are returned from any WebGUI form.
=head1 SYNOPSIS
use WebGUI::FormProcessor;
$value = WebGUI::FormProcessor::process("favoriteColor","selectList","black");
$value = WebGUI::FormProcessor::checkbox("whichOne");
$value = WebGUI::FormProcessor::checkList("dayOfWeek");
$value = WebGUI::FormProcessor::combo("fruit");
$value = WebGUI::FormProcessor::date("endDate");
$value = WebGUI::FormProcessor::dateTime("whenToDoIt");
$value = WebGUI::FormProcessor::email("emailAddress");
$value = WebGUI::FormProcessor::fieldType("fieldType");
$value = WebGUI::FormProcessor::filterContent("javascript");
$value = WebGUI::FormProcessor::float("distance");
$value = WebGUI::FormProcessor::group("groupToPost");
$value = WebGUI::FormProcessor::hidden("wid");
$value = WebGUI::FormProcessor::hiddenList("colors");
$value = WebGUI::FormProcessor::HTMLArea("description");
$value = WebGUI::FormProcessor::integer("size");
$value = WebGUI::FormProcessor::interval("timeToLive");
$value = WebGUI::FormProcessor::password("identifier");
$value = WebGUI::FormProcessor::phone("cellPhone");
$value = WebGUI::FormProcessor::radio("whichOne");
$value = WebGUI::FormProcessor::radioList("dayOfWeek");
$value = WebGUI::FormProcessor::selectList("dayOfWeek");
$value = WebGUI::FormProcessor::template("templateId");
$value = WebGUI::FormProcessor::text("firstName");
$value = WebGUI::FormProcessor::textarea("emailMessage");
$value = WebGUI::FormProcessor::time("wakeupCall");
$value = WebGUI::FormProcessor::url("homepage");
$value = WebGUI::FormProcessor::yesNo("happy");
$value = WebGUI::FormProcessor::zipcode("workZip");
=head1 METHODS
These functions are available from this package:
=cut
#-------------------------------------------------------------------
=head2 checkbox ( name )
Returns either an array of values or a scalar value depending upon what you request.
=over
=item name
The name of the form variable to retrieve.
=back
sub checkbox {
return selectList($_[0]);
}
#-------------------------------------------------------------------
=head2 checkboxList ( name )
Returns either an array of values or a scalar value depending upon what you request.
=over
=item name
The name of the form variable to retrieve.
=back
sub checkboxList {
return selectList($_[0]);
}
#-------------------------------------------------------------------
=head2 combo ( name )
Returns either an array of values or a scalar value depending upon what you request.
=over
=item name
The name of the form variable to retrieve.
=back
sub combo {
if ($session{form}{$_[0]."_new"}) {
return $session{form}{$_[0]."_new"};
}
return selectList($_[0]);
}
#-------------------------------------------------------------------
=head2 date ( name )
Returns an epoch datestamp.
=over
=item name
The name of the form variable to retrieve.
=back
sub date {
return WebGUI::DateTime::setToEpoch($session{form}{$_[0]});
}
#-------------------------------------------------------------------
=head2 dateTime ( name )
Returns an epoch datestamp.
=over
=item name
The name of the form variable to retrieve.
=back
sub dateTime {
return (date($_[0]."_date")+time($_[0]."_time"));
}
#-------------------------------------------------------------------
=head2 email ( name )
Returns an email address.
=over
=item name
The name of the form variable to retrieve.
=back
sub email {
if ($session{form}{$_[0]} =~ /^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$/i) {
return $session{form}{$_[0]};
}
return undef;
}
#-------------------------------------------------------------------
=head2 fieldType ( name )
Returns either an array or a scalar depending upon what you request. Defaults to "text".
=over
=item name
The name of the form variable to retrieve.
=back
sub fieldType {
return (selectList($_[0]) || "text");
}
#-------------------------------------------------------------------
=head2 filter ( name )
Returns a scalar filter type. Defaults to "most".
=over
=item name
The name of the form variable to retrieve.
=back
sub filter {
return ($session{form}{$_[0]} || "most");
}
#-------------------------------------------------------------------
=head2 float ( name )
Returns a floating point (decimal) number. Defaults to "0.0".
=over
=item name
The name of the form variable to retrieve.
=back
sub float {
if ($session{form}{$_[0]} =~ /^[\d\.\-]+$/) {
return $session{form}{$_[0]};
}
return 0.0;
}
#-------------------------------------------------------------------
=head2 hidden ( name )
Returns a string.
=over
=item name
The name of the form variable to retrieve.
=back
sub hidden {
return $session{form}{$_[0]};
}
#-------------------------------------------------------------------
=head2 hiddenList ( name )
Returns either an array or a scalar depending upon what you request.
=over
=item name
The name of the form variable to retrieve.
=back
sub hiddenList {
return selectList($_[0]);
}
#-------------------------------------------------------------------
=head2 HTMLArea ( name )
Returns a string of HTML that has been cleaned of header information.
=over
=item name
The name of the form variable to retrieve.
=back
sub HTMLArea {
return WebGUI::HTML::cleanSegment($session{form}{$_[0]});
}
#-------------------------------------------------------------------
=head2 integer ( name )
Returns an integer. Defaults to "0".
=over
=item name
The name of the form variable to retrieve.
=back
sub integer {
if ($session{form}{$_[0]} =~ /^[\d\-]+$/) {
return $session{form}{$_[0]};
}
return 0;
}
#-------------------------------------------------------------------
=head2 interval ( name )
Returns an interval in seconds. Defaults to "0".
=over
=item name
The name of the form variable to retrieve.
=back
sub interval {
return (WebGUI::DateTime::intervalToSeconds($session{form}{$_[0]."_interval"},$session{form}{$_[0]."_units"}) || 0);
}
#-------------------------------------------------------------------
=head2 password ( name )
Returns a string.
=over
=item name
The name of the form variable to retrieve.
=back
sub password {
return $session{form}{$_[0]};
}
#-------------------------------------------------------------------
=head2 phone ( name )
Returns a string filtered to allow only digits, spaces, and these special characters: + - ( )
=over
=item name
The name of the form variable to retrieve.
=back
sub phone {
if ($session{form}{$_[0]} =~ /^[\d\s\-\+\(\)]+$/) {
return $session{form}{$_[0]};
}
return undef;
}
#-------------------------------------------------------------------
=head2 process ( name, type [ , default ] )
Returns whatever would be the expected result of the method type that was specified.
=over
=item name
The name of the form variable to retrieve.
=item type
The type of form element this variable came from.
=item default
The default value for this variable. If the variable is undefined then the default value will be returned instead.
=back
sub process {
my ($name, $type, $default) = @_;
if (exists $session{form}{$name}) {
return &$type($name);
}
return $default;
}
#-------------------------------------------------------------------
=head2 radio ( name )
Returns a string.
=over
=item name
The name of the form variable to retrieve.
=back
sub radio {
return $session{form}{$_[0]};
}
#-------------------------------------------------------------------
=head2 radioList ( name )
Returns a string.
=over
=item name
The name of the form variable to retrieve.
=back
sub radioList {
return $session{form}{$_[0]};
}
#-------------------------------------------------------------------
=head2 selectList ( name )
Returns either an array or a scalar depending upon what you request.
=over
=item name
The name of the form variable to retrieve.
=back
sub selectList {
my @array = $session{cgi}->param($_[0]);
if ($#array) {
return = \@array;
}
return $session{form}{$_[0]};
}
#-------------------------------------------------------------------
=head2 template ( name )
Returns a template id. Defaults to "1".
=over
=item name
The name of the form variable to retrieve.
=back
sub template {
if ($session{form}{$_[0]} =~ /^\d+$/) {
return $session{form}{$_[0]};
}
return 1;
}
#-------------------------------------------------------------------
=head2 text ( name )
Returns a string of text.
=over
=itemname
The name of the form variable to retrieve.
=back
sub text {
return $session{form}{$_[0]};
}
#-------------------------------------------------------------------
=head2 textarea ( name )
Returns a string of text.
=over
=item name
The name of the form variable to retrieve.
=back
sub textarea {
return $session{form}{$_[0]};
}
#-------------------------------------------------------------------
=head2 time ( name )
Returns the number of seconds since 00:00:00 on a 24 hour clock.
=over
=item name
The name of the form variable to retrieve.
=back
sub time {
return WebGUI::DateTime::timeToEpoch($session{form}{$_[0]});
}
#-------------------------------------------------------------------
=head2 url ( name )
Returns a URL.
=over
=item name
The name of the form variable to retrieve.
=back
sub url {
if ($session{form}{$_[0]} =~ /^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$/i) {
if ($session{form}{$_[0]} =~ /mailto:/) {
return $session{form}{$_[0]};
}
return "mailto:".$session{form}{$_[0]};
} elsif ($session{form}{$_[0]} =~ /:/) {
return $session{form}{$_[0]};
}
return "http://".$session{form}{$_[0]};
}
#-------------------------------------------------------------------
=head2 yesNo ( name )
Returns either a 1 or 0 representing yes and no. Defaults to "0".
=over
=item name
The name of the form variable to retrieve.
=back
sub yesNo {
if ($session{form}{$_[0]} > 0) {
return 1;
}
return 0;
}
#-------------------------------------------------------------------
=head2 zipcode ( name )
Returns a string which allows uppercase alpha characters, digits, spaces, and hypens (dashes).
=over
=item name
The name of the form variable to retrieve.
=back
sub zipcode {
if ($session{form}{$_[0]} =~ /^[A-Z\d\s\-]+$/) {
return $session{form}{$_[0]};
}
return undef;
}
1;

View file

@ -565,7 +565,7 @@ The name field for this form element.
=item types
An array reference of field types to be displayed. The field names are the names of the methods from this forms package. Note that not all field types are supported.
An array reference of field types to be displayed. The field names are the names of the methods from this forms package. Note that not all field types are supported. Defaults to all types.
=item label
@ -607,7 +607,7 @@ sub fieldType {
my ($name, $types, $label, $value, $size, $multiple, $extras, $subtext, $uiLevel) =
rearrange([qw(name types label value size multiple extras subtext uiLevel)], @p);
if (_uiLevelChecksOut($uiLevel)) {
$output = WebGUI::Form::fieldTypes({
$output = WebGUI::Form::fieldType({
"name"=>$name,
"types"=>$types,
"value"=>$value,