Browse Source

Fix validateUnique emitting errors when non-scalar data is received.

While normally the IsUnique() rule can expect sane data as it will have
already been validated, that is not true when the rule is used inside
a validator function. This prevents non-scalar data from getting to the
rule by failing early.

Refs #9088
Mark Story 9 years ago
parent
commit
104f79608a
2 changed files with 10 additions and 1 deletions
  1. 6 1
      src/ORM/Table.php
  2. 4 0
      tests/TestCase/ORM/TableTest.php

+ 6 - 1
src/ORM/Table.php

@@ -2267,8 +2267,13 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
             [$context['field']],
             isset($options['scope']) ? (array)$options['scope'] : []
         );
+        $values = $entity->extract($fields);
+        foreach ($values as $field) {
+            if ($field !== null && !is_scalar($field)) {
+                return false;
+            }
+        }
         $rule = new IsUnique($fields);
-
         return $rule($entity, ['repository' => $this]);
     }
 

+ 4 - 0
tests/TestCase/ORM/TableTest.php

@@ -5608,6 +5608,10 @@ class TableTest extends TestCase
         $validator = new Validator;
         $validator->add('username', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);
         $validator->provider('table', $table);
+
+        $data = ['username' => ['larry', 'notthere']];
+        $this->assertNotEmpty($validator->errors($data));
+
         $data = ['username' => 'larry'];
         $this->assertNotEmpty($validator->errors($data));