Browse Source

Form::validate() throw RuntimeException if validator is invalid

Erwane Breton 4 years ago
parent
commit
3d52b8db65
2 changed files with 24 additions and 3 deletions
  1. 10 3
      src/Form/Form.php
  2. 14 0
      tests/TestCase/Form/FormTest.php

+ 10 - 3
src/Form/Form.php

@@ -140,9 +140,9 @@ class Form implements EventListenerInterface, EventDispatcherInterface, Validato
     /**
      * Set the schema for this form.
      *
-     * @since 4.1.0
      * @param \Cake\Form\Schema $schema The schema to set
      * @return $this
+     * @since 4.1.0
      */
     public function setSchema(Schema $schema)
     {
@@ -158,8 +158,8 @@ class Form implements EventListenerInterface, EventDispatcherInterface, Validato
      * is first built. This hook method lets you configure the
      * schema or load a pre-defined one.
      *
-     * @since 4.1.0
      * @return \Cake\Form\Schema the schema instance.
+     * @since 4.1.0
      */
     public function getSchema(): Schema
     {
@@ -177,9 +177,9 @@ class Form implements EventListenerInterface, EventDispatcherInterface, Validato
      * is first built. This hook method lets you configure the
      * schema or load a pre-defined one.
      *
-     * @deprecated 4.1.0 Use {@link setSchema()}/{@link getSchema()} instead.
      * @param \Cake\Form\Schema|null $schema The schema to set, or null.
      * @return \Cake\Form\Schema the schema instance.
+     * @deprecated 4.1.0 Use {@link setSchema()}/{@link getSchema()} instead.
      */
     public function schema(?Schema $schema = null): Schema
     {
@@ -212,6 +212,7 @@ class Form implements EventListenerInterface, EventDispatcherInterface, Validato
      * @param array $data The data to check.
      * @param array $options The validate options
      * @return bool Whether or not the data is valid.
+     * @throws \RuntimeException If validator is invalid.
      */
     public function validate(array $data, array $options = []): bool
     {
@@ -229,6 +230,12 @@ class Form implements EventListenerInterface, EventDispatcherInterface, Validato
             $validator = $options['validate'];
         }
 
+        if ($validator === null) {
+            throw new RuntimeException(
+                sprintf('validate must be a boolean, a string or an object. Got %s.', getTypeName($options['validate']))
+            );
+        }
+
         $this->_errors = $validator->validate($data);
 
         return count($this->_errors) === 0;

+ 14 - 0
tests/TestCase/Form/FormTest.php

@@ -20,6 +20,7 @@ use Cake\Form\Form;
 use Cake\Form\Schema;
 use Cake\TestSuite\TestCase;
 use Cake\Validation\Validator;
+use RuntimeException;
 use TestApp\Form\AppForm;
 use TestApp\Form\FormSchema;
 
@@ -155,6 +156,19 @@ class FormTest extends TestCase
     }
 
     /**
+     * Test that invalid validate options raise exceptions
+     */
+    public function testValidateInvalidType(): void
+    {
+        $this->expectException(RuntimeException::class);
+        $this->expectExceptionMessage('validate must be a boolean, a string or an object. Got NULL.');
+        $data = ['email' => 'wrong'];
+        $form = new Form();
+        $form->validate($data, ['validate' => null]);
+    }
+
+
+    /**
      * Test the get errors methods.
      */
     public function testGetErrors(): void