Skip to content

Commit

Permalink
Add a configuration option for null uniqe indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
VanTanev committed Aug 24, 2011
1 parent 071f8be commit f643c43
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/validator/sfValidatorPropelUnique.class.php
Expand Up @@ -49,6 +49,8 @@ public function __construct($options = array(), $messages = array())
* * field Field name used by the form, other than the column name
* * primary_key: The primary key column name in Propel field name format (optional, will be introspected if not provided)
* You can also pass an array if the table has several primary keys
* * allow_null_uniques: Whether to allow null values (false by default). If a field is not "NOT NULL" the validator will accept null values.
* Check this for database support: http://troels.arvin.dk/db/rdbms/#constraints-unique
* * connection: The Propel connection to use (null by default)
* * throw_global_error: Whether to throw a global error (false by default) or an error tied to the first field related to the column option array
*
Expand All @@ -61,6 +63,7 @@ protected function configure($options = array(), $messages = array())
$this->addOption('query_methods', array());
$this->addOption('field', null);
$this->addOption('primary_key', null);
$this->addOption('allow_null_uniques', false);
$this->addOption('connection', null);
$this->addOption('throw_global_error', false);

Expand Down Expand Up @@ -101,7 +104,7 @@ protected function doClean($values)
}
}
$tableMap = call_user_func(array($this->getOption('model') . 'Peer', 'getTableMap'));
$nullCols = 0;
$nullColsCount = 0;
foreach ($this->getOption('column') as $i => $column)
{
$name = isset($fields[$i]) ? $fields[$i] : $column;
Expand All @@ -116,14 +119,14 @@ protected function doClean($values)
// handle null unique indexes
if (null === $values[$name] && !$tableMap->getColumn($colName)->isNotNull())
{
$nullCols++;
$nullColsCount++;
continue;
}

$criteria->add($colName, $values[$name]);
}

if ($nullCols != count($this->getOption('column')))
if ( !$this->getOption('allow_null_uniques') || $nullColsCount != count($this->getOption('column')) )
{
$object = $criteria->findOne($this->getOption('connection'));
}
Expand Down

0 comments on commit f643c43

Please sign in to comment.