HTML::Widget::JavaScript::Result - Result Class which supports JavaScript parameter validation


HTML-Widget-JavaScript documentation Contained in the HTML-Widget-JavaScript distribution.

Index


Code Index:

NAME

Top

HTML::Widget::JavaScript::Result - Result Class which supports JavaScript parameter validation

VERSION

Top

Version 0.02

SYNOPSIS

Top

This module adds the ability to output JavaScript validation code to HTML::Widget::Result.

FUNCTIONS

Top

See HTML::Widget::Result.

$self->as_xml

Returns the widget's XML representation, including JavaScript validation code.

AUTHOR

Top

Nilson Santos Figueiredo Júnior, <nilsonsfj at cpan.org>

BUGS

Top

Please report any bugs or feature requests directly to the author. If you ask nicely it will probably get fixed or implemented.

COPYRIGHT & LICENSE

Top


HTML-Widget-JavaScript documentation Contained in the HTML-Widget-JavaScript distribution.
package HTML::Widget::JavaScript::Result;

use warnings;
use strict;

use base 'HTML::Widget::Result';
__PACKAGE__->mk_attr_accessors(qw/onsubmit/);

our $VERSION = '0.02';

sub as_xml {
	my $self = shift;
	
	my $id = $self->name;

	# stop function taken from prototype.js
	my $js_code = <<EOJS;
<script type="text/javascript">
function __html__widget__validate__$id (event) {
	var form = document.getElementById('$id');
	var stop = function() {
		if (event.preventDefault) {
			event.preventDefault();
			event.stopPropagation();
		} else {
			event.returnValue = false;
			event.cancelBubble = true;
		}
	};

EOJS

	for my $constraint ( @{ $self->{_constraints} } ) {
		next unless UNIVERSAL::can($constraint, 'emit_javascript');
		for my $js_const ($constraint->emit_javascript('form')) {
			my $msg = $constraint->message;
			$msg =~ s/'/\\'/g;
			$msg =~ s/\\$/\\\\/;
			$js_code .= "	if $js_const { alert('$msg'); stop(); return false; }\n";
		}
		$js_code .= "\n";
	}

	$js_code .= <<EOJS;
	
	return true;
}
</script>
EOJS
	
	$self->onsubmit("__html__widget__validate__$id(event)");
	return $js_code . $self->SUPER::as_xml;
}

1; # End of HTML::Widget::JavaScript::Result