Public Member Functions | Static Public Member Functions | Data Fields | Protected Member Functions
FormValidation Class Reference

Minimizes form validation code. More...

Public Member Functions

 FormValidation ($method= 'post')
 
 setMethod ($method)
 
 field ($field)
 
 fieldStore ($field)
 
 value ($val)
 
 valueStore ($val)
 
 getValue ()
 
 validate ()
 
 getMessages ()
 
 required ()
 
 minLength ($length)
 
 maxLength ($length)
 
 rangeLength ($min, $max)
 
 pregMatch ($regexp)
 
 email ()
 
 multipleEmail ()
 
 number ()
 
 numeric ()
 
 greaterThan ($value)
 
 lessThan ($value)
 
 equals ($value)
 
 equalsField ($name)
 
 alpha ()
 
 trim ()
 
 toEntities ($constants=null)
 
 toXmlEntities ()
 

Static Public Member Functions

static isEmail ($email)
 
static isNumber ($number)
 

Data Fields

const REQUIRED = 0
 
const MINIMUM_LENGTH = 1
 
const MAXIMUM_LENGTH = 2
 
const RANGE_LENGTH = 3
 
const PREG_MATCH = 4
 
const EMAIL = 5
 
const EMAIL_MULTIPLE = 6
 
const NUMBER = 7
 
const NUMERIC = 8
 
const GREATER_THAN = 9
 
const LESS_THAN = 10
 
const EQUALS = 11
 
const EQUALS_FIELD = 12
 
const ALPHA = 13
 
const ALL_FIELDS = 14
 
 $method
 
 $current_field = null
 
 $current_field_val = null
 
 $messages_store = false
 
 $messages
 

Protected Member Functions

 report ($message_code, $required_value= '')
 

Detailed Description

Minimizes form validation code.

Version
1.2
Author
DinuSV

The FormValidation library is a quick and easy way to validate the fields of a form. The process includes selecting the field, chaining the necessary methods in a try-catch block, and displaying the exception.

A list of the validation functions supported by this library :

$fv = new FormValidation('post'); // Form submission type: post/get
$fv->field('field_name'); // field to validate
$fv->required(); // set this field as required
$fv->minLength(15); // minimum required characters
$fv->maxLength(30); // maximum allowed characters
$fv->rangeLength(15, 30); // field must have length between this interval
$fv->pregMatch('/abc/'); // match field against regular expression
$fv->email(); // check if this field is a valid email
$fv->multipleEmail(); // emails separated by comma
$fv->number(); // separators and numbers allowed
$fv->numeric(); // only numbers allowed
$fv->greaterThan(20); // field >= 20
$fv->lessThan(20); // field =< 20
$fv->equals('abcd'); // field must have the specified value
$fv->equalsField('password_confirm'); // field must be equal to another field
$fv->alpha(); // allow only letters
$fv->trim(); // trim field value
$fv->toEntities(); // convert this fields entities
$fv->toXmlEntities(); // convert ( <, >, &, '' ) to entities

When initializing the library, the constructor takes one optional argument representing the method the form was submited by ( post / get ). The default value is post. If you're autoloading this library, and have a form submitted by get, make sure you change the value with setMethod().

$this->formvalidation->setMethod('get');

This library can validate either selected field using the field() method, or values using the value() method. All validation functions throw an InvalidFieldException if the field does not meet specified criteria. The message can be received from the exception by using the `getMessage() function, and the field name using getFieldName();

try{
$this->formvalidation->field('name')->required();
} catch ( InvalidFieldException $e ) {
echo '<b>' . $e->getFieldName() . ' : </b>' . $e->getMessage();
}

The exception is thrown at the first invalid field met. This makes for printing messages one by one, until all fields have been filled properly. To output all messages within one validation, when selecting the field, 'fieldStore()' can be used instead, making the messages store into an array that can be further retrieved using getMessages(). Similarly for values, the `valueStore() method does the same thing. The received messages are stored in a 2-key array, where the first key is the fields name, and the second is the message counter for the field.

try{
$this->formvalidation->fieldStore('name')->required();
$this->formvalidation->fieldStore('email')->required()->email();
} catch ( InvalidFieldException $e ) {
$messages = $this->formvalidation->getMessages();
foreach ( $messages as $field => $fieldmsgs )
foreach( $fieldmsgs as $message )
echo '<b>' . $e->getFieldName() . ' : </b>' . $e->getMessage();
}

Member Function Documentation

FormValidation (   $method = 'post')

Constructor

Parameters
string$method: [optional] get/post
setMethod (   $method)

Set the method of the form get/pos

Parameters
string$method
Returns
FormValidation : current object
field (   $field)

Set the form field to be validated

Parameters
string$field: the form field name
Returns
FormValidation : current object
fieldStore (   $field)

Set the form field to be validated. All the messages will be stored and no exceptions thrown

Parameters
string$field: the form field name
Returns
FormValidation : current object
value (   $val)

Set the value to be validated.

Parameters
mixed$val
Returns
FormValidation : current object
valueStore (   $val)

Set the value to be validated. All messages will be stored and no exceptions thrown.

Parameters
mixed$val
Returns
FormValidation : current object
getValue ( )

Get the value thats being validated

Exceptions
InvalidArgumentTypeException
Returns
mixed
report (   $message_code,
  $required_value = '' 
)
protected

Helper method used by this class in order to either store a value or throw an exception in case a field is not valid

Parameters
string$message_code: the message to be stored or reported
string$required_value: [optional]
Exceptions
InvalidFieldException
validate ( )

Function validating all fields that have been stored.

Exceptions
InvalidFieldException
Returns
FormValidation : current object
getMessages ( )

Get the received messages when validating this form

Returns
array : messages that have been stored, or null otherwise
required ( )

Set this field as required

Exceptions
InvalidFieldException
Returns
FormValidation : current object
minLength (   $length)

Set this fields minimum length

Parameters
integer$length
Exceptions
InvalidFieldException
Returns
FormValidation : current object
maxLength (   $length)

Set this fields maximum length

Parameters
integer$length
Exceptions
InvalidFieldException
Returns
FormValidation : current object
rangeLength (   $min,
  $max 
)

Set this fields allowed length

Parameters
integer$min: the minimum length this field needs to have in order to be valid
integer$max: the maximum length this field needs to have in order to be valid
Exceptions
InvalidFieldException
Returns
FormValidation : current object
pregMatch (   $regexp)

Set this field to match a regexp expression

Parameters
string$regexp
Exceptions
InvalidFieldException
Returns
FormValidation : current object
email ( )

Set this field to be a valid email adress

Exceptions
InvalidFieldException
Returns
FormValidation : current object
multipleEmail ( )

Set this field to be valid email adresses separated by commas

Exceptions
InvalidFieldException
Returns
FormValidation : current object
number ( )

Set this field to be a valid number, can be separated by spaces, commas, underscores lines, etc.

Exceptions
InvalidFieldException
Returns
FormValidation : current object
numeric ( )

Set this field to be a numeric value

Exceptions
InvalidFieldException
Returns
FormValidation : current object
greaterThan (   $value)

Set this field to be greater than a given value

Parameters
integer$value
Exceptions
InvalidFieldException
Returns
FormValidation : current object
lessThan (   $value)

Set this field to be less than a given value

Parameters
integer$value
Exceptions
InvalidFieldException
Returns
FormValidation : current object
equals (   $value)

Set this field to be equal with another value

Parameters
mixed$value
Exceptions
InvalidFieldException
Returns
FormValidation : current object
equalsField (   $name)

Set this field to be equal to another field

Parameters
string$name: the name of the field
Exceptions
InvalidFieldException
Returns
FormValidation : current object
alpha ( )

Allow only letters for this field

Exceptions
InvalidFieldException
Returns
FormValidation : current object
trim ( )

Trim this field

Returns
FormValidation : current object
toEntities (   $constants = null)

Convert this fields entities

Parameters
integer$constants
Returns
FormValidation : current object
toXmlEntities ( )

Convert this fields xml entities ( <, >, &, " )

Returns
FormValidation : currentt object
static isEmail (   $email)
static

Validates an email address Confitions :

  • false if no '@' symbol
  • false if length before '@' string is smaller than 1 or bigger than 100
  • false if length after '@' string is smaller than 1 or bigger than 255
  • false if before '@' string starts or ends with '.'
  • false if before '@' string has 2 consecutive dots
  • false if before '@' string has illegal chars unless unqoted
  • false if after '@' string contains illegal chars
  • false if after '@' string has 2 consecutive dots
Parameters
string$email
Returns
boolean : true if email is valid, false otherwise
static isNumber (   $number)
static

Check if a parameter is a valid number

Parameters
string$number: the value to check
Returns
boolean : true if it's a number, false otherwise

Field Documentation

const REQUIRED = 0

Required field

const MINIMUM_LENGTH = 1

Minimum characters required

const MAXIMUM_LENGTH = 2

Maximum characters required

const RANGE_LENGTH = 3

Both minimum and maximum characters required

const PREG_MATCH = 4

Regular expression match

const EMAIL = 5

Email type field

const EMAIL_MULTIPLE = 6

Multiple email type field

const NUMBER = 7

Only number-characters allowed

const NUMERIC = 8

Only number-characters and separator symbols allowed

const GREATER_THAN = 9

Number field needs to be greater than value

const LESS_THAN = 10

Number field needs to be less than value

const EQUALS = 11

Field equals value

const EQUALS_FIELD = 12

Field equals field

const ALPHA = 13

Field is only letters

const ALL_FIELDS = 14

All form fields

$method

string : 'post'/'get'

$current_field = null

string : The field to be validated

$current_field_val = null

mixed : The field value

$messages_store = false

bool : Store messages and output the messages at the end or right after an invalid field has been identified. True to store, false otherwise.

$messages

array : Messages to be stored