YaBB3::Validator - The Validator Class.


Name

YaBB3::Validator - The Validator Class.


Synopsis

  use YaBB3::Validator;
  # creation of Validator-object
  my $validator = new YaBB3::Validator;
  # validate $myNumber as numeric.
  $validator->validateNumber( $myNumber, sub { $Y->error("This is not a number!")}) || $Y->error("Please specify a number which is not 0.");
  return if $Y->isError();
  
  # validate multiple values at once
  my $form_validator = YaBB3::Validator->new( {
        'login' => {
                'type' => 'text',
                'required' => 1,
                },
                'password' => {
                'type' => 'password',
                'required' => 1,
                'errormsg' => 'Invalid password specified. It has to be at least 6 characters long and should contain special characters.',
                },
                'cookie_length' => {
                'type' => 'number',
                'required' => 0,
                },
        });
        
    my($login, $password, $expires) = $form_validator->validateFields(['login', $Y->{QUERY}->{login}], ['password', $Y->{QUERY}->{password}], ['cookie_length', $Y->{QUERY}->{cookie_length}]);
        return $Y->errorDisplay("Please correct the following errors.") if $Y->isError();
        
        # Prints a javascript validator that has to be placed within an existing script tag
        print $form_validator->jsValidator()


Overview

The Validator class provides methods for data validation and encoding.


Description

You can provide as parameter a function which should be called als return if the validation fails. If no function is specified the validate methods returns undef. If the validation is successful it returns the number.


Reference


=head2 Used Packages
  YaBB3::Base
  Apache::Util or HTML::Entities

Exports

  Default   $Y $VERSION
  Optional
  Tags

Object Super Classes

  YaBB3::Base

Object Methods

jsValidator

  \%fields                      hash containing the fields to be validated. the key
                        represents the field name the value is another hash:
            type       type of field (must be a value of $_datatypes)
            required   true if the field has to be filled out by the user
  Returns
  $        Javascript code to validate the defined fields in the HTML page.

Generates JavaScript code to validate fields in an HTML page. You can either specify the validation hash while instanciating the validator object with the new constructor or pass the hash directly to this method. The returned code has to be implemented in a function within an existing script tag and called i.e. in onSubmit attribute of a form tag.

validateFields

---

new

  Returns
  /%             The constructed object reference.

Construct and return a new validator object.

validateAlphaNumeric

  $alpha   The alphanumeric text to validate
  $errSub  [opt] Closure called if validation failed
  Returns
  $        The alphanumeric text, with the errSub or undef.

Validates if the parameter is alphanumeric. If validation fails it returns with the as 2nd parameter specified subroutine or returns undef.

validateEMail

Same as validateAlphaNumeric except that we validate an email adress.

validateFilename

Same as validateAlphaNumeric except that we validate a filename.

validateFile

Same as validateAlphaNumeric except that we validate a filename with path.

validateGender

Same as validateAlphaNumeric except that we validate a gender value.

validateIP

Same as validateAlphaNumeric except that we validate an IP.

validateLink

Same as validateAlphaNumeric except that we validate link.

validateNumber

Same as validateAlphaNumeric except that we validate a number.

validateParam

Same as validateAlphaNumeric except that we validate a parameter value.

validatePhoneNumber

Same as validateAlphaNumeric except that we validate a phone number.

validateQueryString

Same as validateAlphaNumeric except that we validate a query string.

validateText

Same as validateAlphaNumeric except that we validate a text. Potentialy malicious characters are encoded.

validateBool

Same as validateAlphaNumeric except that we validate a boolean value.

textlistToArray

Converts a comma serperated list into an array. Removes multiple entries with the same value.

Private Object Methods

<Z>

Package Subroutines

encodeHTML $text A text to encode with HTML entities Returns \$ The new text

Encodes a string with HTML entities to prevent cross-site-scripting. Tries to use Apache::Util and fallbacks to HTML::Entities if not installed.


Author

Torsten Mrotz (tmrotz@yabbforum.com)

Copyright ©2006, YaBB 3 Development Team. All Rights Reserved.
You may distribute this module under the terms of YaBB 3.
 YaBB3::Validator - The Validator Class.