From e127d8f86fa25b935d8d0a0e2402b83c92cc9bbc Mon Sep 17 00:00:00 2001
From: gwoo <gwoo@cakephp.org>
Date: Tue, 22 Jan 2008 07:47:56 +0000
Subject: [PATCH] updating ajax and form closes #3582, tests added

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6401 3807eeeb-6ff5-0310-8944-8be069107fe0
---
 cake/libs/view/helpers/ajax.php               | 42 +++++++------------
 cake/libs/view/helpers/form.php               | 13 +++---
 .../cases/libs/view/helpers/ajax.test.php     |  5 +++
 3 files changed, 27 insertions(+), 33 deletions(-)

diff --git a/cake/libs/view/helpers/ajax.php b/cake/libs/view/helpers/ajax.php
index 5706c6469..0c1791d5c 100644
--- a/cake/libs/view/helpers/ajax.php
+++ b/cake/libs/view/helpers/ajax.php
@@ -68,7 +68,7 @@ class AjaxHelper extends AppHelper {
  *
  * @var array
  */
-	var $ajaxOptions = array('after', 'asynchronous', 'before', 'confirm', 'condition', 'contentType', 'encoding', 'evalScripts', 'failure', 'fallback', 'form', 'indicator', 'insertion', 'interactive', 'loaded', 'loading', 'method', 'onCreate', 'onComplete', 'onException', 'onFailure', 'onInteractive', 'onLoaded', 'onLoading', 'onSuccess', 'onUninitialized', 'parameters', 'position', 'postBody', 'requestHeaders', 'success', 'type', 'update', 'url', 'with');
+	var $ajaxOptions = array('after', 'asynchronous', 'before', 'confirm', 'condition', 'contentType', 'encoding', 'evalScripts', 'failure', 'fallback', 'form', 'indicator', 'insertion', 'interactive', 'loaded', 'loading', 'method', 'onCreate', 'onComplete', 'onException', 'onFailure', 'onInteractive', 'onLoaded', 'onLoading', 'onSuccess', 'onUninitialized', 'parameters', 'position', 'postBody', 'requestHeaders', 'success', 'type', 'update', 'with');
 /**
  * Options for draggable.
  *
@@ -290,43 +290,31 @@ class AjaxHelper extends AppHelper {
  * @return string JavaScript/HTML code
  */
 	function form($params = null, $type = 'post', $options = array()) {
+		$model = false;
 		if (is_array($params)) {
 			extract($params, EXTR_OVERWRITE);
-
-			if (!isset($action)) {
-				$action = null;
-			}
-
-			if (!isset($type)) {
-				$type = 'post';
-			}
-
-			if (!isset($options)) {
-				$options = array();
-			}
-		} else {
-			$action = $params;
 		}
+
+		if (empty($options['url'])) {
+			$options['url'] = array('action' => $params);
+		}
+
 		$htmlOptions = array_merge(
-			array(
-				'id'		=> 'form' . intval(rand()),
-				'action'	=> $action,
-				'onsubmit'	=> "event.returnValue = false; return false;",
-				'type'		=> $type
-			),
-			$this->__getHtmlOptions($options)
+				array(
+					'id' => 'form' . intval(rand()),
+					'onsubmit'	=> "event.returnValue = false; return false;",
+					'type'		=> $type
+				),
+			$this->__getHtmlOptions($options, array('model', 'with'))
 		);
+
 		$options = array_merge(
 			array(
-				'url' => $htmlOptions['action'],
-				'model' => false,
+				'model' => $model,
 				'with' => "Form.serialize('{$htmlOptions['id']}')"
 			),
 			$options
 		);
-		foreach (array_keys($options) as $key) {
-			unset($htmlOptions[$key]);
-		}
 
 		return $this->Form->create($options['model'], $htmlOptions)
 			. $this->Javascript->event("'" . $htmlOptions['id']. "'", 'submit', $this->remoteFunction($options));
diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php
index 960fc2afc..e6bae3fc3 100644
--- a/cake/libs/view/helpers/form.php
+++ b/cake/libs/view/helpers/form.php
@@ -155,11 +155,12 @@ class FormHelper extends AppHelper {
 		$options);
 
 		if (empty($options['url']) || is_array($options['url'])) {
-			$options = (array)$options;
-			if (!empty($model) && $model != $defaultModel) {
-				$controller = Inflector::underscore(Inflector::pluralize($model));
-			} else {
-				$controller = Inflector::underscore($this->params['controller']);
+			if (empty($options['url']['controller'])) {
+				if (!empty($model) && $model != $defaultModel) {
+					$options['url']['controller'] = Inflector::underscore(Inflector::pluralize($model));
+				} elseif (!empty($this->params['controller'])) {
+					$options['url']['controller'] = Inflector::underscore($this->params['controller']);
+				}
 			}
 			if (empty($options['action'])) {
 				$options['action'] = ife($created, 'edit', 'add');
@@ -167,7 +168,7 @@ class FormHelper extends AppHelper {
 
 			$actionDefaults = array(
 				'plugin' => $this->plugin,
-				'controller' => $controller,
+				'controller' => $view->viewPath,
 				'action' => $options['action'],
 				'id' => $id
 			);
diff --git a/cake/tests/cases/libs/view/helpers/ajax.test.php b/cake/tests/cases/libs/view/helpers/ajax.test.php
index 3b9925609..53a333bd6 100644
--- a/cake/tests/cases/libs/view/helpers/ajax.test.php
+++ b/cake/tests/cases/libs/view/helpers/ajax.test.php
@@ -160,6 +160,10 @@ class AjaxTest extends UnitTestCase {
 	function testForm() {
 		$result = $this->Ajax->form('showForm', 'post', array('model' => 'Form', 'url' => array('action' => 'showForm', 'controller' => 'forms'), 'update' => 'form_box'));
 		$this->assertNoPattern('/model=/', $result);
+
+		$result = $this->Ajax->form('showForm', 'post', array('id' => 'MyFormID', 'url' => array('action' => 'showForm', 'controller' => 'forms'), 'update' => 'form_box'));
+		$this->assertPattern('/id="MyFormID"/', $result);
+
 	}
 
 	function testSortable() {
@@ -421,6 +425,7 @@ class AjaxTest extends UnitTestCase {
 
 	function tearDown() {
 		unset($this->Ajax);
+		ClassRegistry::flush();
 	}
 }
 ?>
\ No newline at end of file