浏览代码

FormHelper

euromark 11 年之前
父节点
当前提交
7ee0e3be24
共有 3 个文件被更改,包括 128 次插入0 次删除
  1. 32 0
      docs/Helper/Form.md
  2. 49 0
      src/View/Helper/FormHelper.php
  3. 47 0
      tests/TestCase/View/Helper/FormHelperTest.php

+ 32 - 0
docs/Helper/Form.md

@@ -0,0 +1,32 @@
+# Form Helper
+
+An enhanced FormHelper
+- Allow configuration via Configure `FormConfig`
+- Allow easy enabling/disabling of `novalidate` this way globally throughout all forms
+
+## Configs
+- 'novalidate' => false, // Set to true to disable HTML5 browser validation
+- 'templates' => [...] // Define your own custom default templates for all widgets
+
+## Usage
+Attach it to your controllers like so:
+```php
+public $helpers = ['Tools.Form'];
+```
+
+Alternatively, you can enable it in your AppView class.
+
+### Basic Example
+```php
+// Inside your app.php config:
+$config = [
+	'debug' => true,
+	...
+	'FormConfig' => array(
+		'novalidate' => true,
+		'templates' => array(
+			'dateWidget' => '{{day}}{{month}}{{year}}{{hour}}{{minute}}{{second}}{{meridian}}',
+		)
+	)
+];
+```

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

@@ -0,0 +1,49 @@
+<?php
+namespace Tools\View\Helper;
+
+use Cake\View\Helper\FormHelper as CakeFormHelper;
+use Cake\View\View;
+use Cake\Core\Configure;
+use Cake\Utility\Hash;
+
+/**
+ * Overwrite
+ *
+ */
+class FormHelper extends CakeFormHelper {
+
+	protected $_defaultConfigExt = array(
+		'novalidate' => false
+	);
+
+	/**
+	 * Construct the widgets and binds the default context providers
+	 *
+	 * @param \Cake\View\View $View The View this helper is being attached to.
+	 * @param array $config Configuration settings for the helper.
+	 */
+	public function __construct(View $View, array $config = []) {
+		$this->_defaultConfig += $this->_defaultConfigExt;
+		$defaultConfig = (array)Configure::read('FormConfig');
+		if ($defaultConfig) {
+			$this->_defaultConfig = Hash::merge($this->_defaultConfig, $defaultConfig);
+		}
+		parent::__construct($View, $config);
+	}
+
+	/**
+	 * Overwrite to allow FormConfig Configure settings to be applied.
+	 *
+	 * @param mixed $model 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 model-less form.
+	 * @param array $options An array of html attributes and options.
+	 * @return string An formatted opening FORM tag.
+	 */
+	public function create($model = null, array $options = []) {
+		$defaults = ['novalidate' => $this->_defaultConfig['novalidate']];
+		$options += $defaults;
+		return parent::create($model, $options);
+	}
+
+}

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

@@ -0,0 +1,47 @@
+<?php
+namespace Tools\TestCase\View\Helper;
+
+use Tools\View\Helper\FormHelper;
+use Tools\TestSuite\TestCase;
+use Cake\View\View;
+use Cake\Core\Configure;
+use Cake\Routing\Router;
+
+/**
+ * FormHelper tests
+ */
+class FormHelperTest extends TestCase {
+
+	public $Form;
+
+	public function setUp() {
+		parent::setUp();
+
+		Configure::delete('FormConfig');
+
+		$this->View = new View(null);
+		$this->Form = new FormHelper($this->View);
+	}
+
+	/**
+	 * test novalidate for create
+	 *
+	 * @return void
+	 */
+	public function testCreate() {
+		$expected = 'novalidate="novalidate"';
+
+		$result = $this->Form->create();
+		$this->assertNotContains($expected, $result);
+
+		Configure::write('FormConfig.novalidate', true);
+		$this->Form = new FormHelper($this->View);
+
+		$result = $this->Form->create();
+		$this->assertContains($expected, $result);
+
+		$result = $this->Form->create(null, ['novalidate' => false]);
+		$this->assertNotContains($expected, $result);
+	}
+
+}