Browse Source

Add notEmptyArray()

Mark Story 7 years ago
parent
commit
b8dbbac99b
2 changed files with 69 additions and 1 deletions
  1. 20 0
      src/Validation/Validator.php
  2. 49 1
      tests/TestCase/Validation/ValidatorTest.php

+ 20 - 0
src/Validation/Validator.php

@@ -847,6 +847,26 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
     }
 
     /**
+     * Require a field to be a non-empty array
+     *
+     * This method is equivalent to calling allowEmptyFor() with EMPTY_STRING +
+     * EMPTY_ARRAY flags.
+     *
+     * @param string $field The name of the field.
+     * @param bool|string|callable $when Indicates when the field is allowed to be empty
+     * @param string|null $message The message to show if the field is not
+     * Valid values are false (always), 'create', 'update'. If a callable is passed then
+     * the field will allowed to be empty only when the callback returns false.
+     * @return $this
+     * @since 3.8.0
+     */
+    public function notEmptyArray($field, $message = null, $when = false)
+    {
+        $when = $this->invertWhenClause($when);
+        return $this->allowEmptyFor($field, self::EMPTY_STRING | self::EMPTY_ARRAY, $when, $message);
+    }
+
+    /**
      * Allows a field to be an empty file.
      *
      * This method is equivalent to calling allowEmptyFor() with EMPTY_FILE flag.

+ 49 - 1
tests/TestCase/Validation/ValidatorTest.php

@@ -805,7 +805,7 @@ class ValidatorTest extends TestCase
     public function testNotEmptyString()
     {
         $validator = new Validator();
-        $validator->notEmptyString('title');
+        $validator->notEmptyString('title', 'not empty');
 
         $this->assertFalse($validator->isEmptyAllowed('title', true));
         $this->assertFalse($validator->isEmptyAllowed('title', false));
@@ -819,6 +819,12 @@ class ValidatorTest extends TestCase
 
         $data = ['title' => []];
         $this->assertEmpty($validator->errors($data), 'empty array is no good');
+
+        $expected = [
+            'title' => ['_empty' => 'not empty'],
+        ];
+        $data = ['title' => ''];
+        $this->assertSame($expected, $validator->errors($data, true));
     }
 
     /**
@@ -887,7 +893,15 @@ class ValidatorTest extends TestCase
         ];
         $result = $validator->errors($data);
         $this->assertSame($expected, $result);
+    }
 
+    /**
+     * Test allowEmptyArray with update mode
+     *
+     * @return void
+     */
+    public function testAllowEmptyArrayUpdate()
+    {
         $validator = new Validator();
         $validator->allowEmptyArray('items', 'update', 'message');
         $this->assertFalse($validator->isEmptyAllowed('items', true));
@@ -904,6 +918,40 @@ class ValidatorTest extends TestCase
     }
 
     /**
+     * Tests the notEmptyArray method
+     *
+     * @return void
+     */
+    public function testNotEmptyArray()
+    {
+        $validator = new Validator();
+        $validator->notEmptyArray('items', 'not empty');
+
+        $this->assertFalse($validator->field('items')->isEmptyAllowed());
+
+        $error = [
+            'items' => ['_empty' => 'not empty']
+        ];
+        $data = ['items' => ''];
+        $result = $validator->errors($data);
+        $this->assertSame($error, $result);
+
+        $data = ['items' => null];
+        $result = $validator->errors($data);
+        $this->assertSame($error, $result);
+
+        $data = ['items' => []];
+        $result = $validator->errors($data);
+        $this->assertSame($error, $result);
+
+        $data = [
+            'items' => [1],
+        ];
+        $result = $validator->errors($data);
+        $this->assertEmpty($result);
+    }
+
+    /**
      * Tests the allowEmptyFile method
      *
      * @return void