
Handles validation for fActiveRecord classes
1.0.0b32 | Fixed an array to string conversion notice 9/21/12 |
---|---|
1.0.0b31 | Fixed checkConditionalRule() to require columns that default to an empty string and are currently set to that value 6/14/11 |
1.0.0b30 | Fixed a bug with setMessageOrder() not accepting a variable number of parameters like fValidation::setMessageOrder() does 3/7/11 |
1.0.0b29 | Updated addManyToManyRule() and addOneToManyRule() to prefix any namespace from $class to $related_class if not already present 11/24/10 |
1.0.0b28 | Updated the class to work with the new nested array structure for validation messages 10/3/10 |
1.0.0b27 | Fixed hasValue() to properly detect zero-value floats, made hasValue() internal public 7/26/10 |
1.0.0b26 | Improved the error message for integers to say whole number instead of just number 5/29/10 |
1.0.0b25 | Added addRegexRule(), changed validation messages array to use column name keys 5/26/10 |
1.0.0b24 | Added addRequiredRule() for required columns that aren't automatically handled via schema detection 4/6/10 |
1.0.0b23 | Added support for checking integers and floats to ensure they fit within the range imposed by the database schema 3/17/10 |
1.0.0b22 | Made 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.0b21 | Fixed a bug affecting where conditions with columns that are not null but have a default value 11/3/09 |
1.0.0b20 | Updated code for the new fORMDatabase and fORMSchema APIs 10/28/09 |
1.0.0b19 | Changed SQL statements to use value placeholders, identifier escaping and schema support 10/22/09 |
1.0.0b18 | Fixed checkOnlyOneRule() and checkOneOrMoreRule() to consider blank strings as NULL 8/21/09 |
1.0.0b17 | Added @internal methods removeStringReplacement() and removeRegexReplacement() 7/29/09 |
1.0.0b16 | Backwards 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.0b15 | Added addValidValuesValidationRule() [wb/jt, 2009-07-13] |
1.0.0b14 | Added addStringReplacement() and addRegexReplacement() for simple validation message modification 7/1/09 |
1.0.0b13 | Changed reorderMessages() to compare string in a case-insensitive manner 6/30/09 |
1.0.0b12 | Updated 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.0b11 | Fixed a couple of bugs with validating related records 6/26/09 |
1.0.0b10 | Fixed UNIQUE constraint checking so it is only done once per constraint, fixed some UTF-8 case sensitivity issues 6/17/09 |
1.0.0b9 | Updated code for new fORM API 6/15/09 |
1.0.0b8 | Updated code to use new fValidationException::formatField() method 6/4/09 |
1.0.0b7 | Updated validateRelated() to use new fORMRelated::validate() method and checkRelatedOneOrMoreRule() to use new $related_records structure 6/2/09 |
1.0.0b6 | Changed date/time/timestamp checking from strtotime() to fDate/fTime/fTimestamp for better localization support 6/1/09 |
1.0.0b5 | Fixed a bug in checkOnlyOneRule() where no values would not be flagged as an error 4/23/09 |
1.0.0b4 | Fixed a bug in checkUniqueConstraints() related to case-insensitive columns 2/15/09 |
1.0.0b3 | Implemented proper fix for addManyToManyValidationRule() 12/12/08 |
1.0.0b2 | Fixed a bug with addManyToManyValidationRule() 12/8/08 |
1.0.0b | The initial implementation 8/4/07 |
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.
void addConditionalRule( mixed $class, string|array $main_columns, mixed $conditional_values, string|array $conditional_columns )
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 |
Add a many-to-many rule that requires at least one related record is associated with the current record
void addManyToManyRule( mixed $class, string $related_class, string $route=NULL )
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 |
Adds a one-or-more rule that requires at least one of the columns specified has a value
void addOneOrMoreRule( mixed $class, array $columns )
mixed | $class | The class name or instance of the class the columns exists in |
array | $columns | The columns to check |
Add a one-to-many rule that requires at least one related record is associated with the current record
void addOneToManyRule( mixed $class, string $related_class, string $route=NULL )
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 |
Add an only-one rule that requires exactly one of the columns must have a value
void addOnlyOneRule( mixed $class, array $columns )
mixed | $class | The class name or instance of the class the columns exists in |
array | $columns | The columns to check |
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.
void addRegexReplacement( mixed $class, string $search, string $replace )
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 |
Adds a rule to validate a column against a PCRE regular expression - the rule is not run if the value is NULL
void addRegexRule( mixed $class, string $column, string $regex, string $message )
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 |
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.
void addRequiredRule( mixed $class, array $columns )
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 |
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.
void addStringReplacement( mixed $class, string $search, string $replace )
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 |
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.
void addValidValuesRule( mixed $class, string $column, array $valid_values )
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 |
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.
string hasValue( fSchema $schema, string $class, array &$values, $column, array $columns )
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 |
An error message for the rule
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
void inspect( string $class, string $column, array &$metadata )
string | $class | The class being inspected |
string | $column | The column being inspected |
array | &$metadata | The array of metadata about a column |
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
void removeRegexReplacement( mixed $class, string $search, string $replace )
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 |
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
void removeStringReplacement( mixed $class, string $search, string $replace )
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 |
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
array reorderMessages( string $class, array $messages )
string | $class | The class to reorder messages for |
array | $messages | An array of the messages |
The reordered messages
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
array replaceMessages( string $class, array $messages )
string | $class | The class to reorder messages for |
array | $messages | The array of messages |
The new array of messages
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
void reset( )
Sets a column to be compared in a case-insensitive manner when checking UNIQUE and PRIMARY KEY constraints
void setColumnCaseInsensitive( mixed $class, string $column )
mixed | $class | The class name or instance of the class the column is located in |
string | $column | The column to set as case-insensitive |
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.
void setMessageOrder( mixed $class, array $matches )
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. |
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
array validate( fActiveRecord $object, array $values, array $old_values )
fActiveRecord | $object | The instance of the class to validate |
array | $values | The values to validate |
array | $old_values | The old values for the record |
An array of messages
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
array validateRelated( fActiveRecord $object, array &$values, array &$related_records )
fActiveRecord | $object | The object to validate |
array | &$values | The values for the object |
array | &$related_records | The related records for the object |
An array of messages