Flourish PHP Unframework

fValidation

class, v1.0.0b12

Provides validation routines for standalone forms, such as contact forms

Changes:
1.0.0b12Fixed some method signatures 8/24/11
1.0.0b11Fixed addCallbackRule() to be able to handle multiple rules per field 6/2/11
1.0.0b10Fixed addRegexRule() to be able to handle multiple rules per field 8/30/10
1.0.0b9Enhanced all of the add fields methods to accept one field per parameter, or an array of fields 6/24/10
1.0.0b8Added/fixed support for array-syntax fields names 6/9/10
1.0.0b7Added the ability to pass an array of replacements to addRegexReplacement() and addStringReplacement() 5/31/10
1.0.0b6BackwardsCompatibilityBreak - moved one-or-more required fields from addRequiredFields() to addOneOrMoreRule(), moved conditional required fields from addRequiredFields() to addConditionalRule(), changed returned messages array to have field name keys - added lots of functionality 5/26/10
1.0.0b5Added the $return_messages parameter to validate() and updated code for new fValidationException API 9/17/09
1.0.0b4Changed date checking from strtotime() to fTimestamp for better localization support 6/1/09
1.0.0b3Updated for new fCore API 2/16/09
1.0.0b2Added support for validating date and URL fields 1/23/09
1.0.0bThe initial implementation 6/14/07

Static Methods

::compose() protected

Composes text using fText if loaded

Signature

string compose( string $message, mixed $component [, ... ] )

Parameters

string $message The message to compose
mixed $component [, ... ] A string or number to insert into the message

Returns

The composed and possible translated message

::stringlike() protected

Returns TRUE for non-empty strings, numbers, objects, empty numbers and string-like numbers (such as 0, 0.0, '0')

Signature

boolean stringlike( mixed $value )

Parameters

mixed $value The value to check

Returns

If the value is string-like

Methods

->__get() internal public

Please note: this method is public, however it is primarily intended for internal use by Flourish and will normally not be useful in site/application code

All requests that hit this method should be requests for callbacks

Signature

callback __get( string $method )

Parameters

string $method The method to create a callback for

Returns

The callback for the method requested

->addBooleanFields() public

Adds fields to be checked for 1/0, t/f, true/false, yes/no

Signatures

fValidation addBooleanFields( string $field [, ... ] )

fValidation addBooleanFields( array $fields )

Parameters

string $field [, ... ] A field that should contain a boolean value
array $fields Any number of fields that should contain a boolean value

Returns

The validation object, to allow for method chaining

->addCallbackRule() public

Adds a callback validation of a field, with a custom error message

Signature

fValidation addCallbackRule( string $field, callback $callback, string $message )

Parameters

string $field The field to test with the callback
callback $callback The callback to test the value with - this callback should accept a single string parameter and return a boolean
string $message The error message to return if the regular expression does not match the value

Returns

The validation object, to allow for method chaining

->addConditionalRule() public

Adds fields to be conditionally required if another field has any value, or specific values

Signature

fValidation addConditionalRule( string|array $main_fields, mixed $conditional_values, string|array $conditional_fields )

Parameters

string|array $main_fields The fields(s) to check for a value
mixed $conditional_values If NULL, any value in the main field(s) will trigger the conditional field(s), otherwise the value must match this scalar value or be present in the array of values
string|array $conditional_fields The field(s) that are to be required

Returns

The validation object, to allow for method chaining

->addDateFields() public

Adds form fields to the list of fields to be blank or a valid date

Use addRequiredFields() disallow blank values.

Signatures

fValidation addDateFields( string $field [, ... ] )

fValidation addDateFields( array $fields )

Parameters

string $field [, ... ] A field that should contain a valid date
array $fields Any number of fields that should contain a valid date

Returns

The validation object, to allow for method chaining

->addEmailFields() public

Adds form fields to the list of fields to be blank or a valid email address

Use addRequiredFields() disallow blank values.

Signatures

fValidation addEmailFields( string $field [, ... ] )

fValidation addEmailFields( array $fields )

Parameters

string $field [, ... ] A field that should contain a valid email address
array $fields Any number of fields that should contain a valid email address

Returns

The validation object, to allow for method chaining

->addEmailHeaderFields() public

Adds form fields to be checked for email injection

Every field that is included in email headers should be passed to this method.

Signatures

fValidation addEmailHeaderFields( string $field [, ... ] )

fValidation addEmailHeaderFields( array $fields )

Parameters

string $field [, ... ] A field to be checked for email injection
array $fields Any number of fields to be checked for email injection

Returns

The validation object, to allow for method chaining

->addFileUploadRule() public

Add a file upload field to be validated using an fUpload object

Signatures

fValidation addFileUploadRule( string $field, mixed $index, fUpload $uploader )

fValidation addFileUploadRule( string $field, fUpload $uploader )

Parameters

string $field The field to validate
mixed $index The index for array file upload fields
fUpload $uploader The uploader to validate the field with

Returns

The validation object, to allow for method chaining

->addFloatFields() public

Adds fields to be checked for float values

Signatures

fValidation addFloatFields( string $field [, ... ] )

fValidation addFloatFields( array $fields )

Parameters

string $field [, ... ] A field that should contain a float value
array $fields Any number of fields that should contain a float value

Returns

The validation object, to allow for method chaining

->addIntegerFields() public

Adds fields to be checked for integer values

Signatures

fValidation addIntegerFields( string $field [, ... ] )

fValidation addIntegerFields( array $fields )

Parameters

string $field [, ... ] A field that should contain an integer value
array $fields Any number of fields that should contain an integer value

Returns

The validation object, to allow for method chaining

->addOneOrMoreRule() public

Adds a rule to make sure at least one field of multiple has a value

Signature

fValidation addOneOrMoreRule( string $field, string $field_2 [, ... ] )

Parameters

string $field One of the fields to check for a value
string $field_2 [, ... ] Another field to check for a value

Returns

The validation object, to allow for method chaining

->addOnlyOneRule() public

Adds a rule to make sure at exactly one field of multiple has a value

Signature

fValidation addOnlyOneRule( string $field, string $field_2 [, ... ] )

Parameters

string $field One of the fields to check for a value
string $field_2 [, ... ] Another field to check for a value

Returns

The validation object, to allow for method chaining

->addRegexReplacement() public

Adds a call to preg_replace() for each message

Replacement is done right before the messages are reordered and returned.

If a message is an empty string after replacement, it will be removed from the list of messages.

Signatures

fValidation addRegexReplacement( string $search, string $replace )

fValidation addRegexReplacement( array $replacements )

Parameters

string $search The PCRE regex to search for - see http://php.net/pcre for details
string $replace The string to replace with - all $ and \ are used in back references and must be escaped with a \ when meant literally
array $replacements An associative array with keys being regular expressions to search for and values being the string to replace with

Returns

The validation object, to allow for method chaining

->addRegexRule() public

Adds regular expression validation of a field, with a custom error message

Signature

fValidation addRegexRule( string $field, string $regex, string $message )

Parameters

string $field The field to test with the regular expression
string $regex The PCRE regex to search for - see http://php.net/pcre for details
string $message The error message to return if the regular expression does not match the value

Returns

The validation object, to allow for method chaining

->addRequiredFields() public

Adds form fields to be required

Signatures

fValidation addRequiredFields( string $field [, ... ] )

fValidation addRequiredFields( array $fields )

Parameters

string $field [, ... ] A field to require a value for
array $fields Any number of fields to require a value for

Returns

The validation object, to allow for method chaining

->addStringReplacement() public

Adds a call to str_replace() for each message

Replacement is done right before the messages are reordered and returned.

If a message is an empty string after replacement, it will be removed from the list of messages.

Signatures

fValidation addStringReplacement( string $search, string $replace )

fValidation addStringReplacement( array $replacements )

Parameters

string $search The string to search for
string $replace The string to replace with
array $replacements An associative array with keys being strings to search for and values being the string to replace with

Returns

The validation object, to allow for method chaining

->addURLFields() public

Adds form fields to the list of fields to be blank or a valid URL

Use addRequiredFields() disallow blank values.

Signatures

fValidation addURLFields( string $field [, ... ] )

fValidation addURLFields( array $fields )

Parameters

string $field [, ... ] A field that should contain a valid URL
array $fields Any number of fields that should contain a valid URL

Returns

The validation object, to allow for method chaining

->addValidValuesRule() public

Adds a rule to make sure a field has one of the specified valid values

A strict comparison will be made from the string request value to the array of valid values.

Signature

fValidation addValidValuesRule( string $field, array $valid_values )

Parameters

string $field The field to check the value of
array $valid_values The valid values

Returns

The validation object, to allow for method chaining

->overrideFieldName() public

Allows overriding the default name used for a field in the error message

By default, all fields are referred to by the field name run through fGrammar::humanize(). This may not be correct for acronyms or complex field names.

Signatures

fValidation overrideFieldName( string $field, string $name )

fValidation overrideFieldName( array $field_names )

Parameters

string $field The field to set the custom name for
string $name The custom name for the field
array $field_names An associative array of custom field names where the keys are the field and the values are the names

Returns

The validation object, to allow for method chaining

->setMessageOrder() public

Allows setting the order that the individual errors in a message will be displayed

All string comparisons during the reordering process are done in a case-insensitive manner.

Signature

fValidation setMessageOrder( string $match, string $match_2=NULL [, ... ] )

Parameters

string $match The string match to order first
string $match_2 [, ... ] The string match to order second

Returns

The validation object, to allow for method chaining

->validate() public

Checks for required fields, email field formatting and email header injection using values previously set

Signature

void|array validate( boolean $return_messages=FALSE, boolean $remove_field_names=FALSE )

Parameters

boolean $return_messages If an array of validation messages should be returned instead of an exception being thrown
boolean $remove_field_names If field names should be removed from the returned messages, leaving just the message itself

Returns

If $return_messages is TRUE, an array of validation messages will be returned

Throws

fValidationException
When one of the options set for the object is violated