Permalink
Browse files

BackwardsCompatibilityBreak - fActiveRecord::validate() now uses colu…

…mn names as array keys if messages are returned, the `$validation_messages` parameter for the `pre::validate()` and `post::validate()` hooks now requires array keys be column names, fValidation::validate() now uses field names as array keys if messages are returned, fUpload::validate() no longer returns a `$_FILES` array. fValidation::addRequiredFields() no longer accepts one-or-more rules, instead use fValidation::addOneOrMoreRule(). fValidation::addRequiredFields() no longer accepts conditional rules, instead use fValidation::addConditionalRule().

Fixed ticket #226 with fValidation::overrideFieldName(), fValidation::addStringReplacement() and fValidation::addRegexReplacement(). Fixed ticket #145 with fValidation::addRegexRule(). Fixed ticket #298 with fValidation::addFileUploadRule(). Fixed ticket #373 with `$return_messages` param combined with new `$remove_field_names`/`$remote_column_names` params in fValidation::validate() and fORMValidation::validate().

Added fORMValidation::addRegexRule(), fValidationException::removeFieldNames() and lots of new fValidation functionality. Fixed a bug with checking mimetype of text files with fUpload.
  • Loading branch information...
wbond committed May 27, 2010
1 parent d35ef39 commit 4891f35e60cc313a96a082989dbfa9d1d8fb7a8f
View
@@ -15,7 +15,8 @@
* @package Flourish
* @link http://flourishlib.com/fActiveRecord
*
* @version 1.0.0b63
* @version 1.0.0b64
* @changes 1.0.0b64 BackwardsCompatibilityBreak - changed ::validate()'s returned messages array to have field name keys - added the option to ::validate() to remove field names from messages [wb, 2010-05-26]
* @changes 1.0.0b63 Changed how is_subclass_of() is used to work around a bug in 5.2.x [wb, 2010-04-06]
* @changes 1.0.0b62 Fixed a bug that could cause infinite recursion starting in v1.0.0b60 [wb, 2010-04-02]
* @changes 1.0.0b61 Fixed issues with handling `populate` actions when working with mapped classes [wb, 2010-03-31]
@@ -2824,10 +2825,11 @@ public function store($force_cascade=FALSE)
*
* @throws fValidationException When the record, or one of the associated records, violates one of the validation rules for the class or can not be properly stored in the database
*
* @param boolean $return_messages If an array of validation messages should be returned instead of an exception being thrown
* @param boolean $return_messages If an array of validation messages should be returned instead of an exception being thrown
* @param boolean $remove_column_names If column names should be removed from the returned messages, leaving just the message itself
* @return void|array If $return_messages is TRUE, an array of validation messages will be returned
*/
public function validate($return_messages=FALSE)
public function validate($return_messages=FALSE, $remove_column_names=FALSE)
{
$class = get_class($this);
@@ -2871,6 +2873,9 @@ public function validate($return_messages=FALSE)
$validation_messages = fORMValidation::reorderMessages($class, $validation_messages);
if ($return_messages) {
if ($remove_column_names) {
$validation_messages = fValidationException::removeFieldNames($validation_messages);
}
return $validation_messages;
}
View
@@ -9,7 +9,8 @@
* @package Flourish
* @link http://flourishlib.com/fORMColumn
*
* @version 1.0.0b11
* @version 1.0.0b12
* @changes 1.0.0b12 Changed validation messages array to use column name keys [wb, 2010-05-26]
* @changes 1.0.0b11 Fixed a bug with ::prepareLinkColumn() returning `http://` for empty link columns and not adding `http://` to links that contained a /, but did not start with it [wb, 2010-03-16]
* @changes 1.0.0b10 Fixed ::reflect() to specify the value returned from `set` and `generate` methods, changed ::generate() methods to return the newly generated string [wb, 2010-03-15]
* @changes 1.0.0b9 Changed email columns to be automatically trimmed if they are a value email address surrounded by whitespace [wb, 2010-03-14]
@@ -765,7 +766,7 @@ static public function validateEmailColumns($object, &$values, &$old_values, &$r
continue;
}
if (!preg_match('#^[a-z0-9\\.\'_\\-\\+]+@(?:[a-z0-9\\-]+\.)+[a-z]{2,}$#iD', $values[$column])) {
$validation_messages[] = self::compose(
$validation_messages[$column] = self::compose(
'%sPlease enter an email address in the form name@example.com',
fValidationException::formatField(fORM::getColumnName($class, $column))
);
@@ -804,7 +805,7 @@ static public function validateLinkColumns($object, &$values, &$old_values, &$re
$hostname_regex = '[a-z]+(?:[a-z0-9\-]*[a-z0-9]\.?|\.)*';
$domain_regex = '([a-z]+([a-z0-9\-]*[a-z0-9])?\.)+[a-z]{2,}';
if (!preg_match('#^(https?://(' . $ip_regex . '|' . $hostname_regex . ')(?=/|$)|' . $domain_regex . '(?=/|$)|/)#i', $values[$column])) {
$validation_messages[] = self::compose(
$validation_messages[$column] = self::compose(
'%sPlease enter a link in the form http://www.example.com',
fValidationException::formatField(fORM::getColumnName($class, $column))
);
View
@@ -9,7 +9,8 @@
* @package Flourish
* @link http://flourishlib.com/fORMDate
*
* @version 1.0.0b7
* @version 1.0.0b8
* @changes 1.0.0b8 Changed validation messages array to use column name keys [wb, 2010-05-26]
* @changes 1.0.0b7 Fixed the `set` methods to return the record object in order to be consistent with all other `set` methods [wb, 2010-03-15]
* @changes 1.0.0b6 Fixed an issue with calling a non-existent method on fTimestamp instances [wb, 2009-11-03]
* @changes 1.0.0b5 Updated code for the new fORMDatabase and fORMSchema APIs [wb, 2009-10-28]
@@ -546,13 +547,13 @@ static public function validateTimezoneColumns($object, &$values, &$old_values,
continue;
}
if (!fTimestamp::isValidTimezone($values[$timezone_column])) {
$validation_messages[] = self::compose(
$validation_messages[$timezone_column] = self::compose(
'%sThe timezone specified is invalid',
fValidationException::formatField(fORM::getColumnName($class, $timezone_column))
);
} else {
$validation_messages[] = self::compose(
$validation_messages[$timestamp_column] = self::compose(
'%sPlease enter a date/time',
fValidationException::formatField(fORM::getColumnName($class, $timestamp_column))
);
View
@@ -9,7 +9,8 @@
* @package Flourish
* @link http://flourishlib.com/fORMFile
*
* @version 1.0.0b23
* @version 1.0.0b24
* @changes 1.0.0b24 Changed validation messages array to use column name keys [wb, 2010-05-26]
* @changes 1.0.0b23 Fixed a bug with ::upload() that could cause a method called on a non-object error in relation to the upload directory not being defined [wb, 2010-05-10]
* @changes 1.0.0b22 Updated the TEMP_DIRECTORY constant to not include the trailing slash, code now uses DIRECTORY_SEPARATOR to fix issues on Windows [wb, 2010-04-28]
* @changes 1.0.0b21 Fixed ::set() to perform column inheritance, just like ::upload() does [wb, 2010-03-15]
@@ -1220,9 +1221,11 @@ static public function validate($object, &$values, &$old_values, &$related_recor
foreach (self::$file_upload_columns[$class] as $column => $directory) {
$column_name = fORM::getColumnName($class, $column);
$search_message = self::compose('%sPlease enter a value', fValidationException::formatField($column_name));
$replace_message = self::compose('%sPlease upload a file', fValidationException::formatField($column_name));
$validation_messages = preg_replace('#^' . preg_quote($search_message, '#') . '$#', strtr($replace_message, array('\\' => '\\\\', '$' => '\\$')), $validation_messages);
if (isset($validation_messages[$column])) {
$search_message = self::compose('%sPlease enter a value', fValidationException::formatField($column_name));
$replace_message = self::compose('%sPlease upload a file', fValidationException::formatField($column_name));
$validation_messages[$column] = str_replace($search_message, $replace_message, $validation_messages[$column]);
}
// Grab the error that occured
try {
@@ -1232,7 +1235,7 @@ static public function validate($object, &$values, &$old_values, &$related_recor
}
} catch (fValidationException $e) {
if ($e->getMessage() != self::compose('Please upload a file')) {
$validation_messages[] = fValidationException::formatField($column_name) . $e->getMessage();
$validation_messages[$column] = fValidationException::formatField($column_name) . $e->getMessage();
}
}
}
@@ -1269,4 +1272,4 @@ private function __construct() { }
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
*/
View
@@ -10,7 +10,8 @@
* @package Flourish
* @link http://flourishlib.com/fORMMoney
*
* @version 1.0.0b7
* @version 1.0.0b8
* @changes 1.0.0b8 Changed validation messages array to use column name keys [wb, 2010-05-26]
* @changes 1.0.0b7 Fixed the `set` methods to return the record object in order to be consistent with all other `set` methods [wb, 2010-03-15]
* @changes 1.0.0b6 Fixed duplicate validation messages and fProgrammerException object being thrown when NULL is set [dc-imarc+wb, 2010-03-03]
* @changes 1.0.0b5 Updated code for the new fORMDatabase and fORMSchema APIs [wb, 2009-10-28]
@@ -559,23 +560,18 @@ static public function validateMoneyColumns($object, &$values, &$old_values, &$r
}
// Remove any previous validation warnings
$filtered_messages = array();
$column_name = fValidationException::formatField(fORM::getColumnName($class, $currency_column));
foreach ($validation_messages as $validation_message) {
if (!preg_match('#^' . preg_quote($column_name, '#') . '#', $validation_message)) {
$filtered_messages[] = $validation_message;
}
}
$validation_messages = $filtered_messages;
unset($validation_messages[$column]);
$column_name = fValidationException::formatField(fORM::getColumnName($class, $currency_column));
if ($currency_column && !in_array($values[$currency_column], fMoney::getCurrencies())) {
$validation_messages[] = self::compose(
$validation_messages[$column] = self::compose(
'%sThe currency specified is invalid',
$column_name
);
} else {
$validation_messages[] = self::compose(
$validation_messages[$column] = self::compose(
'%sPlease enter a monetary value',
$column_name
);
View
@@ -2,15 +2,16 @@
/**
* Allows a column in an fActiveRecord class to be a relative sort order column
*
* @copyright Copyright (c) 2008-2009 Will Bond, others
* @copyright Copyright (c) 2008-2010 Will Bond, others
* @author Will Bond [wb] <will@flourishlib.com>
* @author Dan Collins, iMarc LLC [dc-imarc] <dan@imarc.net>
* @license http://flourishlib.com/license
*
* @package Flourish
* @link http://flourishlib.com/fORMOrdering
*
* @version 1.0.0b16
* @version 1.0.0b17
* @changes 1.0.0b17 Changed validation messages array to use column name keys [wb, 2010-05-26]
* @changes 1.0.0b16 Updated the class to allow for multiple ordering columns per class [dc-imarc, 2010-05-10]
* @changes 1.0.0b15 Fixed a bug with ordering columns that are part of a multi-column unique constraint [wb, 2009-11-13]
* @changes 1.0.0b14 Fixed a bug affecting where conditions with columns that are not null but have a default value [wb, 2009-11-03]
@@ -732,9 +733,9 @@ static public function validate($object, &$values, &$old_values, &$related_recor
// Remove any previous validation warnings
$filtered_messages = array();
foreach ($validation_messages as $validation_message) {
if (!preg_match('#^' . str_replace('___', '(.*?)', preg_quote(fValidationException::formatField('___' . $column_name . '___'), '#')) . '#', $validation_message)) {
$filtered_messages[] = $validation_message;
foreach ($validation_messages as $validation_column => $validation_message) {
if (!preg_match('#(^|,)' . preg_quote($column, '#') . '(,|$)#D', $validation_column)) {
$filtered_messages[$validation_column] = $validation_message;
}
}
$validation_messages = $filtered_messages;
@@ -745,10 +746,10 @@ static public function validate($object, &$values, &$old_values, &$related_recor
}
if (!is_numeric($current_value) || strlen((int) $current_value) != strlen($current_value)) {
$validation_messages[] = self::compose('%sPlease enter an integer', fValidationException::formatField($column_name));
$validation_messages[$column] = self::compose('%sPlease enter an integer', fValidationException::formatField($column_name));
} elseif ($current_value < 1) {
$validation_messages[] = self::compose('%sThe value can not be less than 1', fValidationException::formatField($column_name));
$validation_messages[$column] = self::compose('%sThe value can not be less than 1', fValidationException::formatField($column_name));
}
}
@@ -766,7 +767,7 @@ private function __construct() { }
/**
* Copyright (c) 2008-2009 Will Bond <will@flourishlib.com>, others
* Copyright (c) 2008-2010 Will Bond <will@flourishlib.com>, others
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
View
@@ -12,7 +12,8 @@
* @package Flourish
* @link http://flourishlib.com/fORMRelated
*
* @version 1.0.0b28
* @version 1.0.0b29
* @changes 1.0.0b29 Changed validation messages array to use column name keys [wb, 2010-05-26]
* @changes 1.0.0b28 Updated ::associateRecords() to accept just a single fActiveRecord [wb, 2010-05-06]
* @changes 1.0.0b27 Updated the class to force configure classes before peforming actions with them [wb, 2010-03-30]
* @changes 1.0.0b26 Fixed ::reflect() to show the proper return values for `associate`, `link` and `populate` methods [wb, 2010-03-15]
@@ -1590,16 +1591,16 @@ static private function validateOneToStar($class, &$values, &$related_records, $
$records = self::buildRecords($class, $values, $related_records, $related_class, $route);
}
// Ignore validation messages about the primary key since it will be added
$primary_key_name = fValidationException::formatField(fORM::getColumnName($related_class, $route));
$primary_key_regex = '#^' . preg_quote($primary_key_name, '#') . '.*$#D';
fORMValidation::addRegexReplacement($related_class, $primary_key_regex, '');
foreach ($records as $i => $record) {
fRequest::filter($filter, isset($input_keys[$i]) ? $input_keys[$i] : $i);
$record_messages = $record->validate(TRUE);
foreach ($record_messages as $record_message) {
foreach ($record_messages as $column => $record_message) {
// Ignore validation messages about the primary key since it will be added
if ($column == $route) {
continue;
}
$token_field = fValidationException::formatField('__TOKEN__');
$extract_message_regex = '#' . str_replace('__TOKEN__', '(.*?)', preg_quote($token_field, '#')) . '(.*)$#D';
preg_match($extract_message_regex, $record_message, $matches);
@@ -1620,7 +1621,7 @@ static private function validateOneToStar($class, &$values, &$related_records, $
);
}
$messages[] = self::compose(
$messages[$related_table . '::' . $column] = self::compose(
'%1$s%2$s',
fValidationException::formatField($column_name),
$matches[2]
@@ -1629,8 +1630,6 @@ static private function validateOneToStar($class, &$values, &$related_records, $
fRequest::unfilter();
}
fORMValidation::removeRegexReplacement($related_class, $primary_key_regex, '');
return $messages;
}
@@ -1655,7 +1654,7 @@ static private function validateManyToMany($class, $related_class, $route, $rela
foreach ($related_records as $record) {
if ((is_object($record) && !$record->exists()) || !$record) {
$messages[] = self::compose(
$messages[fORM::tablize($related_class)] = self::compose(
'%sPlease select a %3$s',
fValidationException::formatField(
self::compose(
Oops, something went wrong.

0 comments on commit 4891f35

Please sign in to comment.