Skip to content
Permalink
Browse files

Make allowMultipleNulls a constructor argument.

Previously this option was implemented as an additional context
parameter. That seemed a bit strange as the option could and probably
should be a constructor argument. Given that the option is only relevant
to the IsUnique rule there is no reason for the RulesChecker to be
handling it.
  • Loading branch information...
markstory committed May 30, 2016
1 parent 933f814 commit e02b8845b8b1dbc62d548c1fa8cecf005438061c
Showing with 28 additions and 8 deletions.
  1. +10 −3 src/ORM/Rule/IsUnique.php
  2. +9 −2 src/ORM/RulesChecker.php
  3. +9 −3 tests/TestCase/ORM/RulesCheckerIntegrationTest.php
@@ -29,14 +29,22 @@ class IsUnique
*/
protected $_fields;
/**
* The options to use.
*
* @var array
*/
protected $_options;
/**
* Constructor.
*
* @param array $fields The list of fields to check uniqueness for
*/
public function __construct(array $fields)
public function __construct(array $fields, array $options = [])
{
$this->_fields = $fields;
$this->_options = $options + ['allowMultipleNulls' => true];
}
/**
@@ -52,8 +60,7 @@ public function __invoke(EntityInterface $entity, array $options)
if (!$entity->extract($this->_fields, true)) {
return true;
}
$options += ['allowMultipleNulls' => true];
$allowMultipleNulls = $options['allowMultipleNulls'];
$allowMultipleNulls = $this->_options['allowMultipleNulls'];
$alias = $options['repository']->alias();
$conditions = $this->_alias($alias, $entity->extract($this->_fields), $allowMultipleNulls);
@@ -40,11 +40,18 @@ class RulesChecker extends BaseRulesChecker
* ```
*
* @param array $fields The list of fields to check for uniqueness.
* @param string|null $message The error message to show in case the rule does not pass.
* @param string|array|null $message The error message to show in case the rule does not pass. Can
* also be an array of options. When an array, the 'message' key can be used to provide a message.
* @return callable
*/
public function isUnique(array $fields, $message = null)
{
$options = [];
if (is_array($message)) {
$options = $message + ['message' => null];
$message = $options['message'];
unset($options['message']);
}
if (!$message) {
if ($this->_useI18n) {
$message = __d('cake', 'This value is already in use');
@@ -54,7 +61,7 @@ public function isUnique(array $fields, $message = null)
}
$errorField = current($fields);
return $this->_addError(new IsUnique($fields), '_isUnique', compact('errorField', 'message'));
return $this->_addError(new IsUnique($fields, $options), '_isUnique', compact('errorField', 'message'));
}
/**
@@ -389,10 +389,13 @@ public function testIsUniqueAllowMultipleNulls()
$table = TableRegistry::get('SpecialTags');
$rules = $table->rulesChecker();
$rules->add($rules->isUnique(['author_id']), ['allowMultipleNulls' => false]);
$rules->add($rules->isUnique(['author_id'], [
'allowMultipleNulls' => false,
'message' => 'All fields are required'
]));
$this->assertFalse($table->save($entity));
$this->assertEquals(['_isUnique' => 'This value is already in use'], $entity->errors('author_id'));
$this->assertEquals(['_isUnique' => 'All fields are required'], $entity->errors('author_id'));
$entity->author_id = 11;
$this->assertSame($entity, $table->save($entity));
@@ -418,7 +421,10 @@ public function testIsUniqueMultipleFieldsAllowMultipleNulls()
$table = TableRegistry::get('SpecialTags');
$rules = $table->rulesChecker();
$rules->add($rules->isUnique(['author_id', 'article_id'], 'Nope'), ['allowMultipleNulls' => false]);
$rules->add($rules->isUnique(['author_id', 'article_id'], [
'allowMultipleNulls' => false,
'message' => 'Nope'
]));
$this->assertFalse($table->save($entity));
$this->assertEquals(['author_id' => ['_isUnique' => 'Nope']], $entity->errors());

0 comments on commit e02b884

Please sign in to comment.
You can’t perform that action at this time.