ソースを参照

Add a setter for FormHelper::context()

Having a setter makes changing context mid form possible. This is
sometimes useful when forms are partially filled with data from plugins.

Refs #4149
mark_story 11 年 前
コミット
d1f55ca2f0

+ 5 - 1
src/View/Helper/FormHelper.php

@@ -2211,9 +2211,13 @@ class FormHelper extends Helper {
  *
  * If there is no active form null will be returned.
  *
+ * @param \Cake\View\Form\ContextInterface|null Either the new context when setting, or null to get.
  * @return null|\Cake\View\Form\ContextInterface The context for the form.
  */
-	public function context() {
+	public function context($context = null) {
+		if ($context instanceof ContextInterface) {
+			$this->_context = $context;
+		}
 		return $this->_getContext();
 	}
 

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

@@ -6171,4 +6171,18 @@ class FormHelperTest extends TestCase {
 		$this->assertNotEquals('<input>', $this->Form->templater()->get('input'));
 	}
 
+/**
+ * Test the context method.
+ *
+ * @return void
+ */
+	public function testContext() {
+		$result = $this->Form->context();
+		$this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
+
+		$mock = $this->getMock('Cake\View\Form\ContextInterface');
+		$this->assertSame($mock, $this->Form->context($mock));
+		$this->assertSame($mock, $this->Form->context());
+	}
+
 }