From 7de927eb4631b52ee4d48088586de0326cb5d09e Mon Sep 17 00:00:00 2001 From: "mariano.iglesias" Date: Sun, 13 May 2007 12:07:23 +0000 Subject: [PATCH] Fixing and adding tests for ticket #2551 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5071 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/view/helpers/form.php | 40 +++++++++---- .../cases/libs/view/helpers/form.test.php | 56 +++++++++++++++++++ 2 files changed, 86 insertions(+), 10 deletions(-) diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php index 305b2106a..17895e954 100644 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -356,7 +356,15 @@ class FormHelper extends AppHelper { } $text = Inflector::humanize($text); } - return $this->output(sprintf($this->Html->tags['label'], $this->domId($tagName), $this->_parseAttributes($attributes), $text)); + + if (isset($attributes['for'])) { + $labelFor = $attributes['for']; + unset($attributes['for']); + } else { + $labelFor = $this->domId($tagName); + } + + return $this->output(sprintf($this->Html->tags['label'], $labelFor, $this->_parseAttributes($attributes), $text)); } /** * Will display all the fields passed in an array expects tagName as an array key @@ -502,16 +510,28 @@ class FormHelper extends AppHelper { $label = $options['label']; unset($options['label']); } - if (is_array($label)) { - $labelText = null; - if (isset($label['text'])) { - $labelText = $label['text']; - unset($label['text']); + + if ($label !== false) { + $labelAttributes = array(); + + if (in_array($options['type'], array('date', 'datetime'))) { + $labelFor = $this->domId(implode('.', array_filter(array($this->model(), $this->field())))); + $labelAttributes = array( 'for' => $labelFor . 'Month' ); } - $out = $this->label(null, $labelText, $label); - $label = $labelText; - } elseif ($label !== false) { - $out = $this->label(null, $label); + + if (is_array($label)) { + $labelText = null; + if (isset($label['text'])) { + $labelText = $label['text']; + unset($label['text']); + } + + $labelAttributes = am($labelAttributes, $label); + } else { + $labelText = $label; + } + + $out = $this->label(null, $labelText, $labelAttributes); } $error = null; diff --git a/cake/tests/cases/libs/view/helpers/form.test.php b/cake/tests/cases/libs/view/helpers/form.test.php index 9a9a98209..61ebc6c1c 100644 --- a/cake/tests/cases/libs/view/helpers/form.test.php +++ b/cake/tests/cases/libs/view/helpers/form.test.php @@ -61,6 +61,7 @@ return new Set(array( array('name' => 'id', 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), array('name' => 'name', 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), + array('name' => 'published', 'type' => 'date', 'null' => true, 'default' => null, 'length' => null), array('name' => 'created', 'type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), array('name' => 'updated', 'type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) )); @@ -664,6 +665,61 @@ class FormHelperTest extends CakeTestCase { $result = $this->Form->create('Contact', array('id' => 'TestId')); $this->assertPattern('/id="TestId"/', $result); } + + function testFormMagicInput() { + $result = $this->Form->create('Contact'); + $this->assertPattern('/^$/', $result); + + $result = $this->Form->input('Contact.name'); + $this->assertPattern('/^
' . + '