![]() |
|
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= '') | |
Minimizes form validation code.
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 :
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 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();
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.
FormValidation | ( | $method = 'post' | ) |
Constructor
string | $method | : [optional] get/post |
setMethod | ( | $method | ) |
field | ( | $field | ) |
Set the form field to be validated
string | $field | : the form field name |
fieldStore | ( | $field | ) |
Set the form field to be validated. All the messages will be stored and no exceptions thrown
string | $field | : the form field name |
value | ( | $val | ) |
valueStore | ( | $val | ) |
Set the value to be validated. All messages will be stored and no exceptions thrown.
mixed | $val |
getValue | ( | ) |
|
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
string | $message_code | : the message to be stored or reported |
string | $required_value | : [optional] |
InvalidFieldException |
validate | ( | ) |
Function validating all fields that have been stored.
InvalidFieldException |
getMessages | ( | ) |
Get the received messages when validating this form
required | ( | ) |
minLength | ( | $length | ) |
Set this fields minimum length
integer | $length |
InvalidFieldException |
maxLength | ( | $length | ) |
Set this fields maximum length
integer | $length |
InvalidFieldException |
rangeLength | ( | $min, | |
$max | |||
) |
Set this fields allowed length
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 |
InvalidFieldException |
pregMatch | ( | $regexp | ) |
Set this field to match a regexp expression
string | $regexp |
InvalidFieldException |
( | ) |
Set this field to be a valid email adress
InvalidFieldException |
multipleEmail | ( | ) |
Set this field to be valid email adresses separated by commas
InvalidFieldException |
number | ( | ) |
Set this field to be a valid number, can be separated by spaces, commas, underscores lines, etc.
InvalidFieldException |
numeric | ( | ) |
Set this field to be a numeric value
InvalidFieldException |
greaterThan | ( | $value | ) |
Set this field to be greater than a given value
integer | $value |
InvalidFieldException |
lessThan | ( | $value | ) |
Set this field to be less than a given value
integer | $value |
InvalidFieldException |
equals | ( | $value | ) |
Set this field to be equal with another value
mixed | $value |
InvalidFieldException |
equalsField | ( | $name | ) |
Set this field to be equal to another field
string | $name | : the name of the field |
InvalidFieldException |
alpha | ( | ) |
Allow only letters for this field
InvalidFieldException |
trim | ( | ) |
Trim this field
toEntities | ( | $constants = null | ) |
toXmlEntities | ( | ) |
Convert this fields xml entities ( <, >, &, " )
|
static |
Validates an email address Confitions :
string |
|
static |
Check if a parameter is a valid number
string | $number | : the value to check |
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