Browse Source

Allow passing ContextInterface instance to Form::create().

ADmad 8 years ago
parent
commit
3b0c29d3ec
2 changed files with 14 additions and 8 deletions
  1. 12 8
      src/View/Helper/FormHelper.php
  2. 2 0
      tests/TestCase/View/Helper/FormHelperTest.php

+ 12 - 8
src/View/Helper/FormHelper.php

@@ -327,9 +327,9 @@ class FormHelper extends Helper
      * - `valueSources` The sources that values should be read from. See FormHelper::setValueSources()
      * - `templateVars` Provide template variables for the formStart template.
      *
-     * @param mixed $context The context for which the form is being defined. Can
-     *   be an ORM entity, ORM resultset, or an array of meta data. You can use false or null
-     *   to make a context-less form.
+     * @param mixed $context The context for which the form is being defined.
+     *   Can be a ContextInterface instance, ORM entity, ORM resultset, or an
+     *   array of meta data. You can use false or null to make a context-less form.
      * @param array $options An array of html attributes and options.
      * @return string An formatted opening FORM tag.
      * @link https://book.cakephp.org/3.0/en/views/helpers/form.html#Cake\View\Helper\FormHelper::create
@@ -338,12 +338,16 @@ class FormHelper extends Helper
     {
         $append = '';
 
-        if (empty($options['context'])) {
-            $options['context'] = [];
+        if ($context instanceof ContextInterface) {
+            $this->context($context);
+        } else {
+            if (empty($options['context'])) {
+                $options['context'] = [];
+            }
+            $options['context']['entity'] = $context;
+            $context = $this->_getContext($options['context']);
+            unset($options['context']);
         }
-        $options['context']['entity'] = $context;
-        $context = $this->_getContext($options['context']);
-        unset($options['context']);
 
         $isCreate = $context->isCreate();
 

+ 2 - 0
tests/TestCase/View/Helper/FormHelperTest.php

@@ -415,6 +415,7 @@ class FormHelperTest extends TestCase
             ]
         ];
         $form = new Form();
+        $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
 
         return [
             'entity' => [$entity, 'Cake\View\Form\EntityContext'],
@@ -425,6 +426,7 @@ class FormHelperTest extends TestCase
             'form' => [$form, 'Cake\View\Form\FormContext'],
             'none' => [null, 'Cake\View\Form\NullContext'],
             'false' => [false, 'Cake\View\Form\NullContext'],
+            'custom' => [$custom, get_class($custom)]
         ];
     }