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
This commit is contained in:
mariano.iglesias 2007-05-13 12:07:23 +00:00
parent 2cac3f7c1d
commit 7de927eb46
2 changed files with 86 additions and 10 deletions

View file

@ -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;

View file

@ -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('/^<form\s+id="ContactAddForm"\s+method="post"\s+action="\/contacts\/add\/"\s*>$/', $result);
$result = $this->Form->input('Contact.name');
$this->assertPattern('/^<div class="input">' .
'<label for="ContactName">Name<\/label>' .
'<input name="data\[Contact\]\[name\]" type="text" maxlength="255" value="" id="ContactName" \/>'.
'<\/div>$/', $result);
$result = $this->Form->input('Contact.name', array('div' => false));
$this->assertPattern('/^<label for="ContactName">Name<\/label>' .
'<input name="data\[Contact\]\[name\]" type="text" maxlength="255" value="" id="ContactName" \/>$/', $result);
$result = $this->Form->input('Contact.non_existing');
$this->assertPattern('/^<div class="input">' .
'<label for="ContactNonExisting">Non Existing<\/label>' .
'<input name="data\[Contact\]\[non_existing\]" type="text" value="" id="ContactNonExisting" \/>'.
'<\/div>$/', $result);
$result = $this->Form->input('Contact.published', array('div' => false));
$this->assertPattern('/^<label for="ContactPublishedMonth">Published<\/label>' .
'<select name="data\[Contact\]\[published_month\]"\s+id="ContactPublishedMonth">/', $result);
$result = $this->Form->input('Contact.updated', array('div' => false));
$this->assertPattern('/^<label for="ContactUpdatedMonth">Updated<\/label>' .
'<select name="data\[Contact\]\[updated_month\]"\s+id="ContactUpdatedMonth">/', $result);
}
function testFormMagicInputLabel() {
$result = $this->Form->create('Contact');
$this->assertPattern('/^<form\s+id="ContactAddForm"\s+method="post"\s+action="\/contacts\/add\/"\s*>$/', $result);
$result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
$this->assertPattern('/^<input name="data\[Contact\]\[name\]" type="text" maxlength="255" value="" id="ContactName" \/>$/', $result);
$result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
$this->assertPattern('/^<label for="ContactName">My label<\/label>' .
'<input name="data\[Contact\]\[name\]" type="text" maxlength="255" value="" id="ContactName" \/>$/', $result);
$result = $this->Form->input('Contact.name', array('div' => false, 'label' => array('class' => 'mandatory')));
$this->assertPattern('/^<label for="ContactName" class="mandatory">Name<\/label>' .
'<input name="data\[Contact\]\[name\]" type="text" maxlength="255" value="" id="ContactName" \/>$/', $result);
$result = $this->Form->input('Contact.name', array('div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')));
$this->assertPattern('/^<label for="ContactName" class="mandatory">My label<\/label>' .
'<input name="data\[Contact\]\[name\]" type="text" maxlength="255" value="" id="ContactName" \/>$/', $result);
$result = $this->Form->input('Contact.name', array('div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')));
$this->assertPattern('/^<label for="my_id">Name<\/label>' .
'<input name="data\[Contact\]\[name\]" type="text" id="my_id" maxlength="255" value="" \/>$/', $result);
}
function testFormEnd() {
$this->assertEqual($this->Form->end(), '</form>');