diff --git a/lib/WebGUI/Form/Textarea.pm b/lib/WebGUI/Form/Textarea.pm index ce21e38e7..5a9769aa4 100644 --- a/lib/WebGUI/Form/Textarea.pm +++ b/lib/WebGUI/Form/Textarea.pm @@ -62,27 +62,34 @@ Style attributes besides width and height which should be specified using the ab A boolean indicating whether the text area can be reized by users. Defaults to 1. +=head4 maxlength + +The maximum number of characters to allow in this field. If not defined, will not do any limiting. + =cut sub definition { - my $class = shift; - my $session = shift; - my $definition = shift || []; - push(@{$definition}, { + my $class = shift; + my $session = shift; + my $definition = shift || []; + push @{$definition}, { height=>{ defaultValue=> 150 - }, + }, width=>{ defaultValue=> 400 - }, + }, style=>{ defaultValue => undef, - }, + }, resizable => { defaultValue => 1, - }, - }); - return $class->SUPER::definition($session, $definition); + }, + maxlength => { + defaultValue => '' + }, + }; + return $class->SUPER::definition($session, $definition); } #------------------------------------------------------------------- @@ -138,7 +145,26 @@ sub toHtml { my ($style, $url) = $self->session->quick(qw(style url)); my $styleAttribute = "width: ".$width."px; height: ".$height."px; ".$self->get("style"); $style->setRawHeadTags(qq||); - my $out = ''; + my $out = '' + ; + + # Add the maxlength script + $style->setScript( + $url->extras( 'yui/build/yahoo-dom-event/yahoo-dom-event.js' ), + { text => 'text/javascript' }, + ); + $style->setScript( + $url->extras( 'yui-webgui/build/form/textarea.js' ), + { type => 'text/javascript' }, + ); + $style->setRawHeadTags( q| + + | ); + if ($self->get("resizable")) { $style->setLink($url->extras("resize.css"), {type=>"text/css", rel=>"stylesheet"}); $style->setLink($url->extras("resize-skin.css"), {type=>"text/css", rel=>"stylesheet"}); diff --git a/www/extras/yui-webgui/build/form/textarea.js b/www/extras/yui-webgui/build/form/textarea.js new file mode 100644 index 000000000..e90035696 --- /dev/null +++ b/www/extras/yui-webgui/build/form/textarea.js @@ -0,0 +1,39 @@ + +// Initialize namespace +if (typeof WebGUI == "undefined") { + var WebGUI = {}; +} +if (typeof WebGUI.Form == "undefined") { + WebGUI.Form = {}; +} +WebGUI.Form.Textarea = {}; + +/**************************************************************************** + * + * WebGUI.Form.Textarea + * Scripts for the textarea control. + * + */ + +WebGUI.Form.Textarea.checkMaxLength += function () { + var maxLength = this.getAttribute('maxlength'); + var currentLength = this.value.length; + if (currentLength > maxLength) { + this.value = this.value.substring( 0, maxLength ); + alert( "This field can only contain " + maxLength + " characters" ); + } +} + +WebGUI.Form.Textarea.setMaxLength += function () { + var x = document.getElementsByTagName('textarea'); + for ( var i = 0; i < x.length; i++ ) { + if (x[i].getAttribute('maxlength')) { + YAHOO.util.Event.addListener( x[i], "change", WebGUI.Form.Textarea.checkMaxLength ); + YAHOO.util.Event.addListener( x[i], "keyup", WebGUI.Form.Textarea.checkMaxLength ); + } + } +} + +