Flourish PHP Unframework

fORMValidation

static class, v1.0.0b32

Handles validation for fActiveRecord classes

Changes:
1.0.0b32Fixed an array to string conversion notice 9/21/12
1.0.0b31Fixed checkConditionalRule() to require columns that default to an empty string and are currently set to that value 6/14/11
1.0.0b30Fixed a bug with setMessageOrder() not accepting a variable number of parameters like fValidation::setMessageOrder() does 3/7/11
1.0.0b29Updated addManyToManyRule() and addOneToManyRule() to prefix any namespace from $class to $related_class if not already present 11/24/10
1.0.0b28Updated the class to work with the new nested array structure for validation messages 10/3/10
1.0.0b27Fixed hasValue() to properly detect zero-value floats, made hasValue() internal public 7/26/10
1.0.0b26Improved the error message for integers to say whole number instead of just number 5/29/10
1.0.0b25Added addRegexRule(), changed validation messages array to use column name keys 5/26/10
1.0.0b24Added addRequiredRule() for required columns that aren't automatically handled via schema detection 4/6/10
1.0.0b23Added support for checking integers and floats to ensure they fit within the range imposed by the database schema 3/17/10
1.0.0b22Made the value checking for one-or-more and only-one rules more robust when detecting the absence of a value 12/17/09
1.0.0b21Fixed a bug affecting where conditions with columns that are not null but have a default value 11/3/09
1.0.0b20Updated code for the new fORMDatabase and fORMSchema APIs 10/28/09
1.0.0b19Changed SQL statements to use value placeholders, identifier escaping and schema support 10/22/09
1.0.0b18Fixed checkOnlyOneRule() and checkOneOrMoreRule() to consider blank strings as NULL 8/21/09
1.0.0b17Added @internal methods removeStringReplacement() and removeRegexReplacement() 7/29/09
1.0.0b16Backwards Compatibility Break - renamed addConditionalValidationRule() to addConditionalRule(), addManyToManyValidationRule() to addManyToManyRule(), addOneOrMoreValidationRule() to addOneOrMoreRule(), addOneToManyValidationRule() to addOneToManyRule(), addOnlyOneValidationRule() to addOnlyOneRule(), addValidValuesValidationRule() to addValidValuesRule() 7/13/09
1.0.0b15Added addValidValuesValidationRule() [wb/jt, 2009-07-13]
1.0.0b14Added addStringReplacement() and addRegexReplacement() for simple validation message modification 7/1/09
1.0.0b13Changed reorderMessages() to compare string in a case-insensitive manner 6/30/09
1.0.0b12Updated addConditionalValidationRule() to allow any number of $main_columns, and if any of those have a matching value, the condtional columns will be required 6/30/09
1.0.0b11Fixed a couple of bugs with validating related records 6/26/09
1.0.0b10Fixed UNIQUE constraint checking so it is only done once per constraint, fixed some UTF-8 case sensitivity issues 6/17/09
1.0.0b9Updated code for new fORM API 6/15/09
1.0.0b8Updated code to use new fValidationException::formatField() method 6/4/09
1.0.0b7Updated validateRelated() to use new fORMRelated::validate() method and checkRelatedOneOrMoreRule() to use new $related_records structure 6/2/09
1.0.0b6Changed date/time/timestamp checking from strtotime() to fDate/fTime/fTimestamp for better localization support 6/1/09
1.0.0b5Fixed a bug in checkOnlyOneRule() where no values would not be flagged as an error 4/23/09
1.0.0b4Fixed a bug in checkUniqueConstraints() related to case-insensitive columns 2/15/09
1.0.0b3Implemented proper fix for addManyToManyValidationRule() 12/12/08
1.0.0b2Fixed a bug with addManyToManyValidationRule() 12/8/08
1.0.0bThe initial implementation 8/4/07

Static Methods

::addConditionalRule() public

Adds a conditional rule

If a non-empty value is found in one of the $main_columns, or if specified, a value from the $conditional_values array, all of the $conditional_columns will also be required to have a value.

Signature

void addConditionalRule( mixed $class, string|array $main_columns, mixed $conditional_values, string|array $conditional_columns )

Parameters

mixed $class The class name or instance of the class this rule applies to
string|array $main_columns The column(s) to check for a value
mixed $conditional_values If NULL, any value in the main column will trigger the conditional column(s), otherwise the value must match this scalar value or be present in the array of values
string|array $conditional_columns The column(s) that are to be required

::addManyToManyRule() public

Add a many-to-many rule that requires at least one related record is associated with the current record

Signature

void addManyToManyRule( mixed $class, string $related_class, string $route=NULL )

Parameters

mixed $class The class name or instance of the class to add the rule for
string $related_class The name of the related class
string $route The route to the related class

::addOneOrMoreRule() public

Adds a one-or-more rule that requires at least one of the columns specified has a value

Signature

void addOneOrMoreRule( mixed $class, array $columns )

Parameters

mixed $class The class name or instance of the class the columns exists in
array $columns The columns to check

::addOneToManyRule() public

Add a one-to-many rule that requires at least one related record is associated with the current record

Signature

void addOneToManyRule( mixed $class, string $related_class, string $route=NULL )

Parameters

mixed $class The class name or instance of the class to add the rule for
string $related_class The name of the related class
string $route The route to the related class

::addOnlyOneRule() public

Add an only-one rule that requires exactly one of the columns must have a value

Signature

void addOnlyOneRule( mixed $class, array $columns )

Parameters

mixed $class The class name or instance of the class the columns exists in
array $columns The columns to check

::addRegexReplacement() public

Adds a call to preg_replace() for each message

Regex replacement is done after the post::validate() hook, and right before the messages are reordered.

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

Signature

void addRegexReplacement( mixed $class, string $search, string $replace )

Parameters

mixed $class The class name or instance of the class the columns exists in
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

::addRegexRule() public

Adds a rule to validate a column against a PCRE regular expression - the rule is not run if the value is NULL

Signature

void addRegexRule( mixed $class, string $column, string $regex, string $message )

Parameters

mixed $class The class name or instance of the class the columns exists in
string $column The column to match with the regex
string $regex The PCRE regex to match against - see http://php.net/pcre for details
string $message The message to use if the value does not match the regular expression

::addRequiredRule() public

Requires that a column have a non-NULL value

Before using this method, try setting the database column to NOT NULL and remove any default value. Such a configuration will trigger the same functionality as this method, and will enforce the rule on the database level for any other code that queries it.

Signature

void addRequiredRule( mixed $class, array $columns )

Parameters

mixed $class The class name or instance of the class the column(s) exists in
array $columns The column or columns to check - each column will require a value

::addStringReplacement() public

Adds a call to str_replace() for each message

String replacement is done after the post::validate() hook, and right before the messages are reordered.

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

Signature

void addStringReplacement( mixed $class, string $search, string $replace )

Parameters

mixed $class The class name or instance of the class the columns exists in
string $search The string to search for
string $replace The string to replace with

::addValidValuesRule() public

Restricts a column to having only a value from the list of valid values

Please note that NULL values are always allowed, even if not listed in the $valid_values array, if the column is not set as NOT NULL.

This functionality can also be accomplished by added a CHECK constraint on the column in the database, or using a MySQL ENUM data type.

Signature

void addValidValuesRule( mixed $class, string $column, array $valid_values )

Parameters

mixed $class The class name or instance of the class this rule applies to
string $column The column to validate
array $valid_values The valid values to check - NULL values are always allows if the column is not set to NOT NULL

::hasValue() 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

Checks to see if a columns has a value, but based on the schema and if the column allows NULL

If the columns allows NULL values, than anything other than NULL will be returned as TRUE. If the column does not allow NULL and the value is anything other than the "empty" value for that data type, then TRUE will be returned.

The values that are considered "empty" for each data type are as follows. Please note that there is no "empty" value for dates, times or timestamps.

  • Blob: ''
  • Boolean: FALSE
  • Float: 0.0
  • Integer: 0
  • String: ''

Signature

string hasValue( fSchema $schema, string $class, array &$values, $column, array $columns )

Parameters

fSchema $schema The schema object for the table
string $class The class the column is part of
array &$values An associative array of all values for the record
array $columns The column to check
$column

Returns

An error message for the rule

::inspect() 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

Adds metadata about features added by this class

Signature

void inspect( string $class, string $column, array &$metadata )

Parameters

string $class The class being inspected
string $column The column being inspected
array &$metadata The array of metadata about a column

::removeRegexReplacement() 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

Removes a regex replacement

Signature

void removeRegexReplacement( mixed $class, string $search, string $replace )

Parameters

mixed $class The class name or instance of the class the columns exists in
string $search The string to search for
string $replace The string to replace with

::removeStringReplacement() 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

Removes a string replacement

Signature

void removeStringReplacement( mixed $class, string $search, string $replace )

Parameters

mixed $class The class name or instance of the class the columns exists in
string $search The string to search for
string $replace The string to replace with

::reorderMessages() 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

Reorders list items in an html string based on their contents

Signature

array reorderMessages( string $class, array $messages )

Parameters

string $class The class to reorder messages for
array $messages An array of the messages

Returns

The reordered messages

::replaceMessages() 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

Takes a list of messages and performs string and regex replacements on them

Signature

array replaceMessages( string $class, array $messages )

Parameters

string $class The class to reorder messages for
array $messages The array of messages

Returns

The new array of messages

::reset() 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

Resets the configuration of the class

Signature

void reset( )

::setColumnCaseInsensitive() public

Sets a column to be compared in a case-insensitive manner when checking UNIQUE and PRIMARY KEY constraints

Signature

void setColumnCaseInsensitive( mixed $class, string $column )

Parameters

mixed $class The class name or instance of the class the column is located in
string $column The column to set as case-insensitive

::setMessageOrder() public

Allows setting the order that the list items in a message will be displayed

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

Signature

void setMessageOrder( mixed $class, array $matches )

Parameters

mixed $class The class name or an instance of the class to set the message order for
array $matches This should be an ordered array of strings. If a line contains the string it will be displayed in the relative order it occurs in this array.

::validate() 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

Validates values for an fActiveRecord object against the database schema and any additional rules that have been added

Signature

array validate( fActiveRecord $object, array $values, array $old_values )

Parameters

fActiveRecord $object The instance of the class to validate
array $values The values to validate
array $old_values The old values for the record

Returns

An array of messages

::validateRelated() 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

Validates related records for an fActiveRecord object

Signature

array validateRelated( fActiveRecord $object, array &$values, array &$related_records )

Parameters

fActiveRecord $object The object to validate
array &$values The values for the object
array &$related_records The related records for the object

Returns

An array of messages