Stevan Little - HTML.Form.Validator-0.01

Documentation | Source

NAME

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

SYNOPSIS

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

DESCRIPTION

This class is similar in API to the HTML.Form.Validator.Element class, but it can be used to combine any number of HTML.Form.Validator.Element objects and perform tests of arbitrary complexity on them.

This is mainly a reuse mechanism, whereby you can easily combine a number of elements into a single validator.

PROPERTIES

elements

This is the HTML.Form.Validator.Element objects to be validated against.

error

This is the error message to be used in the case of validation failure.

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.CompoundElement (error, error_handler)

This will create and return a new HTML.Form.Validator.CompoundElement object. The error argument is required, but the error_handler is optional.

NOTE: There is no name like in the HTML.Form.Validator.Element object because this class does not represent a single HTML form element, but a collection of them.

addElement (element)

This adds a given element (which is a HTML.Form.Validator.Element or HTML.Form.Validator.CompoundElement (yes you could nest these) object instance) to be validated with later.

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 method which will preform any complex validation on the elements. It should return true for success, and false for failure.

focusElement (form_object)

This method is used to bring focus to one of the elements if the validation fails.

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.CompoundElement = function (error, error_handler) {
	this.elements = [];
	this.error = error;
	this.error_handler = error_handler || function (error) { alert(error) };	
}
HTML.Form.Validator.CompoundElement.VERSION = '0.01';

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

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

HTML.Form.Validator.CompoundElement.prototype.addElement = function(element){
	this.elements[this.elements.length] = element;
}

// overide this method as nessecary
HTML.Form.Validator.CompoundElement.prototype.test = function(form_object){
	for (var i = 0; i < this.elements.length; i++){
		if (!this.elements[i].validate(form_object)){
			return false;
		}
	}
	return true;
}

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

/*

=pod

=head1 NAME

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

=head1 SYNOPSIS

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

=head1 DESCRIPTION

This class is similar in API to the C<HTML.Form.Validator.Element> class, but it can be used to combine any number
of C<HTML.Form.Validator.Element> objects and perform tests of arbitrary complexity on them. 

This is mainly a reuse mechanism, whereby you can easily combine a number of elements into a single validator.

=head1 PROPERTIES

=over 4

=item B<elements>

This is the C<HTML.Form.Validator.Element> objects to be validated against.

=item B<error>

This is the error message to be used in the case of validation failure.

=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.CompoundElement (error, error_handler)>

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

NOTE: There is no C<name> like in the C<HTML.Form.Validator.Element> object because this class does
not represent a single HTML form element, but a collection of them.

=item B<addElement (element)>

This adds a given C<element> (which is a C<HTML.Form.Validator.Element> or C<HTML.Form.Validator.CompoundElement>
(yes you could nest these) object instance) to be validated with later.

=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 method which will preform any complex validation on the C<elements>. It should
return true for success, and false for failure.

=item B<focusElement (form_object)>

This method is used to bring focus to one of the C<elements> if the validation fails. 

=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

*/