Stevan Little - HTML.Form.Validator-0.01

Documentation | Source

NAME

HTML.Form.Validator.Element - A framework for HTML Form Validation

SYNOPSIS

  // these will be added to a HTML.Form.Validator instance
  var first_name = new HTML.Form.Validator.Element('first_name', 'You must fill out a first name!');
  var last_name = new HTML.Form.Validator.Element('last_name', 'You must fill out a last name!');

DESCRIPTION

This is a simple base class from which most of the validation elements will be derived. It has a very simple API, part of which can be overridden to provide customized behavior.

PROPERTIES

name

This is the name of the HTML form element being validated.

error

This is the error message to be used if the element does not pass the validation.

error_handler

This is a function which will be called if the element does not pass the validation. If this is not assigned, then a default is provided for you which looks like this:

  function (error) { alert(error) };

METHODS

new HTML.Form.Validator.Element (name, error, error_handler)

This will create and return a new HTML.Form.Validator.Element object. The name and error arguments are required, but the error_handler is optional.

handleError (form_object)

This method is not normally overridden, it will be called if the validation fails. It's primary responsibilty is to call the error_handler and to bring focus to the errant form element.

validate (form_object)

This method also will not normally be overloaded. It will attempt validate the element by calling test. If it fails, then it will call handleError and return false, if it succeeds it returns true.

test (form_object)

This is the simplest method to override. It is the method which will preform the validation. It should return true for success, and false for failure.

getValue (form_object)

This method is used to extract the value from the form element. It is usually used by test for this purpose.

focusElement (form_object)

This method is what brings focus to the given element if the validation fails. It should be overriden to handle each specific form element, or of you wanted to disable this behavior.

AUTHOR

stevan little, <stevan@iinteractive.com>

COPYRIGHT AND LICENSE

Copyright 2005 by Infinity Interactive, Inc.

http://www.iinteractive.com

This library is free software; you can redistribute it and/or modify it under the same terms as Perl.

// set up the namespace
if (HTML                == undefined) var HTML                = function () {};
if (HTML.Form           == undefined)     HTML.Form           = function () {};
if (HTML.Form.Validator == undefined)     HTML.Form.Validator = function () {};

HTML.Form.Validator.Element = function (name, error, error_handler) {
	this.name = name;
	this.error = error;
	this.error_handler = error_handler || function (error) { alert(error) };
}
HTML.Form.Validator.Element.VERSION = '0.01';

// methods
HTML.Form.Validator.Element.prototype.handleError = function (form_object) {
	this.error_handler(this.error);
	this.focusElement(form_object);
}

HTML.Form.Validator.Element.prototype.validate = function (form_object) {
	if (!this.test(form_object)){
		this.handleError(form_object);
		return false;
	}
	else{
		return true;
	}
}

// over-ride these methods as nessecary
HTML.Form.Validator.Element.prototype.getValue = function(form_object){
	return form_object.elements[this.name].value;
}	

HTML.Form.Validator.Element.prototype.focusElement = function(form_object){
	form_object.elements[this.name].focus();
}

HTML.Form.Validator.Element.prototype.test = function(form_object){
	return this.getValue(form_object);
}

/*

=pod

=head1 NAME

HTML.Form.Validator.Element - A framework for HTML Form Validation

=head1 SYNOPSIS

  // these will be added to a HTML.Form.Validator instance
  var first_name = new HTML.Form.Validator.Element('first_name', 'You must fill out a first name!');
  var last_name = new HTML.Form.Validator.Element('last_name', 'You must fill out a last name!');

=head1 DESCRIPTION

This is a simple base class from which most of the validation elements will be derived. It has 
a very simple API, part of which can be overridden to provide customized behavior. 

=head1 PROPERTIES

=over 4

=item B<name>

This is the name of the HTML form element being validated.

=item B<error>

This is the error message to be used if the element does not pass the validation.

=item B<error_handler>

This is a function which will be called if the element does not pass the validation. If this is 
not assigned, then a default is provided for you which looks like this:

  function (error) { alert(error) };

=back

=head1 METHODS

=over 4

=item B<new HTML.Form.Validator.Element (name, error, error_handler)>

This will create and return a new C<HTML.Form.Validator.Element> object. The C<name> and C<error> arguments
are required, but the C<error_handler> is optional.

=item B<handleError (form_object)>

This method is not normally overridden, it will be called if the validation fails. It's primary responsibilty
is to call the C<error_handler> and to bring focus to the errant form element.

=item B<validate (form_object)>

This method also will not normally be overloaded. It will attempt validate the element by calling C<test>.
If it fails, then it will call C<handleError> and return false, if it succeeds it returns true.

=item B<test (form_object)>

This is the simplest method to override. It is the method which will preform the validation. It should
return true for success, and false for failure.

=item B<getValue (form_object)>

This method is used to extract the value from the form element. It is usually used by C<test> for this purpose.

=item B<focusElement (form_object)>

This method is what brings focus to the given element if the validation fails. It should be overriden to handle
each specific form element, or of you wanted to disable this behavior.

=back

=head1 AUTHOR

stevan little, E<lt>stevan@iinteractive.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright 2005 by Infinity Interactive, Inc.

L<http://www.iinteractive.com>

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl. 

=cut

*/