Browse Source

Add method and enctype options to FormHelper::create()

Merge branch 'FormHelperEncType' into master.

Refs #9565
Mark Story 9 years ago
parent
commit
134288caf4
2 changed files with 34 additions and 0 deletions
  1. 10 0
      src/View/Helper/FormHelper.php
  2. 24 0
      tests/TestCase/View/Helper/FormHelperTest.php

+ 10 - 0
src/View/Helper/FormHelper.php

@@ -320,11 +320,14 @@ class FormHelper extends Helper
      *
      * - `type` Form method defaults to autodetecting based on the form context. If
      *   the form context's isCreate() method returns false, a PUT request will be done.
+     * - `method` Set the form's method attribute explicitly.
      * - `action` The controller action the form submits to, (optional). Use this option if you
      *   don't need to change the controller from the current request's controller. Deprecated since 3.2, use `url`.
      * - `url` The URL the form submits to. Can be a string or a URL array. If you use 'url'
      *    you should leave 'action' undefined.
      * - `encoding` Set the accept-charset encoding for the form. Defaults to `Configure::read('App.encoding')`
+     * - `enctype` Set the form encoding explicitly. By default `type => file` will set `enctype`
+     *   to `multipart/form-data`.
      * - `templates` The templates you want to use for this form. Any templates will be merged on top of
      *   the already loaded templates. This option can either be a filename in /config that contains
      *   the templates you want to load, or an array of templates to use.
@@ -415,6 +418,13 @@ class FormHelper extends Helper
             default:
                 $htmlAttributes['method'] = 'post';
         }
+        if (isset($options['method'])) {
+            $htmlAttributes['method'] = strtolower($options['method']);
+        }
+        if (isset($options['enctype'])) {
+            $htmlAttributes['enctype'] = strtolower($options['enctype']);
+        }
+
         $this->requestType = strtolower($options['type']);
 
         if (!empty($options['encoding'])) {

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

@@ -491,6 +491,30 @@ class FormHelperTest extends TestCase
     }
 
     /**
+     * Test explicit method/enctype options.
+     *
+     * Explicit method overwrites inferred method from 'type'
+     *
+     * @return void
+     */
+    public function testCreateExplictMethodEnctype()
+    {
+        $encoding = strtolower(Configure::read('App.encoding'));
+        $result = $this->Form->create(false, [
+            'type' => 'get',
+            'method' => 'put',
+            'enctype' => 'multipart/form-data'
+        ]);
+        $expected = ['form' => [
+            'method' => 'put',
+            'action' => '/articles/add',
+            'enctype' => 'multipart/form-data',
+            'accept-charset' => $encoding
+        ]];
+        $this->assertHtml($expected, $result);
+    }
+
+    /**
      * Test create() with the templates option.
      *
      * @return void