Browse Source

Add notEmptyTime()

Mark Story 7 years ago
parent
commit
12a1bbc413
2 changed files with 109 additions and 30 deletions
  1. 22 2
      src/Validation/Validator.php
  2. 87 28
      tests/TestCase/Validation/ValidatorTest.php

+ 22 - 2
src/Validation/Validator.php

@@ -970,6 +970,26 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
     }
 
     /**
+     * Require a field to be a non-empty time.
+     *
+     * Opposite to allowEmptyTime()
+     *
+     * @param string $field The name of the field.
+     * @param string|null $message The message to show if the field is not
+     * @param bool|string|callable $when Indicates when the field is allowed to be empty
+     *   Valid values are false, 'create', 'update'. If a callable is passed then
+     *   the field will allowed to be empty only when the callback returns true.
+     * @return $this
+     * @since 3.8.0
+     * @see \Cake\Validation\Validator::allowEmptyTime()
+     */
+    public function notEmptyTime($field, $message = null, $when = false)
+    {
+        $when = $this->invertWhenClause($when);
+        return $this->allowEmptyFor($field, self::EMPTY_STRING | self::EMPTY_TIME, $when, $message);
+    }
+
+    /**
      * Allows a field to be an empty date/time.
      *
      * Empty date values are `null`, `''`, `[]` and arrays where all values are `''`
@@ -980,8 +1000,8 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
      *
      * @param string $field The name of the field.
      * @param bool|string|callable $when Indicates when the field is allowed to be empty
-     * Valid values are true, false, 'create', 'update'. If a callable is passed then
-     * the field will allowed to be empty only when the callback returns true.
+     *   Valid values are true, false, 'create', 'update'. If a callable is passed then
+     *   the field will allowed to be empty only when the callback returns false.
      * @param string|null $message The message to show if the field is not
      * @return $this
      * @since 3.7.0

+ 87 - 28
tests/TestCase/Validation/ValidatorTest.php

@@ -1149,6 +1149,71 @@ class ValidatorTest extends TestCase
     }
 
     /**
+     * Tests the notEmptyDate method
+     *
+     * @return void
+     */
+    public function testNotEmptyDate()
+    {
+        $validator = new Validator();
+        $validator->notEmptyDate('date', 'required field');
+
+        $this->assertFalse($validator->isEmptyAllowed('date', true));
+        $this->assertFalse($validator->isEmptyAllowed('date', false));
+
+        $error = ['date' => ['_empty' => 'required field']];
+        $data = [
+            'date' => [
+                'year' => '',
+                'month' => '',
+                'day' => ''
+            ],
+        ];
+        $result = $validator->errors($data);
+        $this->assertSame($error, $result);
+
+        $data = ['date' => ''];
+        $result = $validator->errors($data);
+        $this->assertSame($error, $result);
+
+        $data = ['date' => null];
+        $result = $validator->errors($data);
+        $this->assertSame($error, $result);
+
+        $data = ['date' => []];
+        $result = $validator->errors($data);
+        $this->assertSame($error, $result);
+
+        $data = [
+            'date' => [
+                'year' => 2019,
+                'month' => 2,
+                'day' => 17
+            ]
+        ];
+        $result = $validator->errors($data);
+        $this->assertEmpty($result);
+    }
+
+    /**
+     * Test notEmptyDate with update mode
+     *
+     * @return void
+     */
+    public function testNotEmptyDateUpdate()
+    {
+        $validator = new Validator();
+        $validator->notEmptyDate('date', 'message', 'update');
+        $this->assertTrue($validator->isEmptyAllowed('date', true));
+        $this->assertFalse($validator->isEmptyAllowed('date', false));
+
+        $data = ['date' => null];
+        $expected = ['date' => ['_empty' => 'message']];
+        $this->assertSame($expected, $validator->errors($data, false));
+        $this->assertEmpty($validator->errors($data, true));
+    }
+
+    /**
      * Tests the allowEmptyTime method
      *
      * @return void
@@ -1203,68 +1268,62 @@ class ValidatorTest extends TestCase
     }
 
     /**
-     * Tests the notEmptyDate method
+     * Tests the notEmptyTime method
      *
      * @return void
      */
-    public function testNotEmptyDate()
+    public function testNotEmptyTime()
     {
         $validator = new Validator();
-        $validator->notEmptyDate('date', 'required field');
+        $validator->notEmptyTime('time', 'required field');
 
-        $this->assertFalse($validator->isEmptyAllowed('date', true));
-        $this->assertFalse($validator->isEmptyAllowed('date', false));
+        $this->assertFalse($validator->isEmptyAllowed('time', true));
+        $this->assertFalse($validator->isEmptyAllowed('time', false));
 
-        $error = ['date' => ['_empty' => 'required field']];
+        $error = ['time' => ['_empty' => 'required field']];
         $data = [
-            'date' => [
-                'year' => '',
-                'month' => '',
-                'day' => ''
+            'time' => [
+                'hour' => '',
+                'minute' => '',
+                'second' => '',
             ],
         ];
         $result = $validator->errors($data);
         $this->assertSame($error, $result);
 
-        $data = ['date' => ''];
+        $data = ['time' => ''];
         $result = $validator->errors($data);
         $this->assertSame($error, $result);
 
-        $data = ['date' => null];
+        $data = ['time' => null];
         $result = $validator->errors($data);
         $this->assertSame($error, $result);
 
-        $data = ['date' => []];
+        $data = ['time' => []];
         $result = $validator->errors($data);
         $this->assertSame($error, $result);
 
-        $data = [
-            'date' => [
-                'year' => 2019,
-                'month' => 2,
-                'day' => 17
-            ]
-        ];
+        $data = ['time' => ['hour' => 12, 'minute' => 12, 'second' => 12]];
         $result = $validator->errors($data);
         $this->assertEmpty($result);
     }
 
     /**
-     * Test notEmptyDate with update mode
+     * Test notEmptyTime with update mode
      *
      * @return void
      */
-    public function testNotEmptyDateUpdate()
+    public function testNotEmptyTimeUpdate()
     {
         $validator = new Validator();
-        $validator->notEmptyDate('date', 'message', 'update');
-        $this->assertTrue($validator->isEmptyAllowed('date', true));
-        $this->assertFalse($validator->isEmptyAllowed('date', false));
+        $validator->notEmptyTime('time', 'message', 'update');
+        $this->assertTrue($validator->isEmptyAllowed('time', true));
+        $this->assertFalse($validator->isEmptyAllowed('time', false));
 
-        $data = ['date' => null];
-        $expected = ['date' => ['_empty' => 'message']];
-        $this->assertSame($expected, $validator->errors($data, false));
+        $data = ['time' => null];
+        $expected = ['time' => ['_empty' => 'message']];
         $this->assertEmpty($validator->errors($data, true));
+        $this->assertSame($expected, $validator->errors($data, false));
     }
 
     /**