updating FormHelper to use fieldset instead of p. Inline styles are still used on these elements. We are not promoting the use of inline style, merely attempting to make life easier. Also, this adds a fieldset to multiple checkboxes if there is a group and also wraps the multiple checkboxes in divs and provides proper label for and input id attributes. also some changes to generic css to handle markup. Closes #3703

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6142 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-12-12 00:46:21 +00:00
parent b6b2696488
commit 98859c8c72
4 changed files with 77 additions and 37 deletions

View file

@ -220,9 +220,19 @@ fieldset legend {
font-size: 160%; font-size: 160%;
font-weight: bold; font-weight: bold;
} }
fieldset fieldset {
margin-top: 0px;
margin-bottom: 20px;
padding: 16px 0;
}
fieldset fieldset legend { fieldset fieldset legend {
font-size: 120%; font-size: 120%;
font-weight: normal; font-weight: normal;
margin-left: 20px;
}
fieldset fieldset div {
clear: left;
margin: 0 20px;
} }
form div { form div {
clear: both; clear: both;

View file

@ -221,9 +221,19 @@ fieldset legend {
font-size: 160%; font-size: 160%;
font-weight: bold; font-weight: bold;
} }
fieldset fieldset {
margin-top: 0px;
margin-bottom: 20px;
padding: 16px 0;
}
fieldset fieldset legend { fieldset fieldset legend {
font-size: 120%; font-size: 120%;
font-weight: normal; font-weight: normal;
margin-left: 20px;
}
fieldset fieldset div {
clear: left;
margin: 0 20px;
} }
form div { form div {
clear: both; clear: both;

View file

@ -203,9 +203,11 @@ class FormHelper extends AppHelper {
$htmlAttributes = array_merge($options, $htmlAttributes); $htmlAttributes = array_merge($options, $htmlAttributes);
if (isset($this->params['_Token']) && !empty($this->params['_Token'])) { if (isset($this->params['_Token']) && !empty($this->params['_Token'])) {
$append .= '<p style="display: none;">';
$append .= $this->hidden('_Token.key', array('value' => $this->params['_Token']['key'], 'id' => 'Token' . mt_rand())); $append .= $this->hidden('_Token.key', array('value' => $this->params['_Token']['key'], 'id' => 'Token' . mt_rand()));
$append .= '</p>'; }
if (!empty($append)) {
$append = '<fieldset style="display:none;">'.$append.'</fieldset>';
} }
$this->setEntity($model . '.', true); $this->setEntity($model . '.', true);
@ -271,7 +273,7 @@ class FormHelper extends AppHelper {
*/ */
function secure($fields) { function secure($fields) {
if (!empty($fields)) { if (!empty($fields)) {
$append = '<p style="display: none;">'; $append = '<fieldset style="display:none;">';
foreach ($fields as $key => $value) { foreach ($fields as $key => $value) {
if (strpos($key, '_') !== 0) { if (strpos($key, '_') !== 0) {
@ -285,7 +287,7 @@ class FormHelper extends AppHelper {
} }
ksort($fields); ksort($fields);
$append .= $this->hidden('_Token.fields', array('value' => urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt'))), 'id' => 'TokenFields' . mt_rand())); $append .= $this->hidden('_Token.fields', array('value' => urlencode(Security::hash(serialize($fields) . Configure::read('Security.salt'))), 'id' => 'TokenFields' . mt_rand()));
$append .= '</p>'; $append .= '</fieldset>';
return $append; return $append;
} }
return null; return null;
@ -1421,13 +1423,21 @@ class FormHelper extends AppHelper {
$htmlOptions = array(); $htmlOptions = array();
if (is_array($title) && (!isset($title['name']) || !isset($title['value']))) { if (is_array($title) && (!isset($title['name']) || !isset($title['value']))) {
if (!empty($name)) { if (!empty($name)) {
if ($attributes['style'] === 'checkbox') {
$select[] = $this->Html->tags['fieldsetend'];
} else{
$select[] = $this->Html->tags['optiongroupend']; $select[] = $this->Html->tags['optiongroupend'];
}
$parents[] = $name; $parents[] = $name;
} }
$select = array_merge($select, $this->__selectOptions($title, $selected, $parents, $showParents, $attributes)); $select = array_merge($select, $this->__selectOptions($title, $selected, $parents, $showParents, $attributes));
if (!empty($name)) { if (!empty($name)) {
if ($attributes['style'] === 'checkbox') {
$select[] = sprintf($this->Html->tags['fieldsetstart'], $name);
} else{
$select[] = sprintf($this->Html->tags['optiongroup'], $name, ''); $select[] = sprintf($this->Html->tags['optiongroup'], $name, '');
} }
}
$name = null; $name = null;
} elseif (is_array($title)) { } elseif (is_array($title)) {
$htmlOptions = $title; $htmlOptions = $title;
@ -1443,17 +1453,27 @@ class FormHelper extends AppHelper {
$htmlOptions['selected'] = 'selected'; $htmlOptions['selected'] = 'selected';
} }
} }
if ($showParents || (!in_array($title, $parents))) { if ($showParents || (!in_array($title, $parents))) {
$title = ife($attributes['escape'], h($title), $title); $title = ife($attributes['escape'], h($title), $title);
if ($attributes['style'] === 'checkbox') { if ($attributes['style'] === 'checkbox') {
$htmlOptions['value'] = $name; $htmlOptions['value'] = $name;
$label = array();
$tagName = Inflector::camelize($this->model() . '_' . $this->field() . '_'.Inflector::underscore($name));
$htmlOptions['id'] = $tagName;
$label = array('for' => $tagName);
if (isset($htmlOptions['checked']) && $htmlOptions['checked'] === true) { if (isset($htmlOptions['checked']) && $htmlOptions['checked'] === true) {
$label['class'] = 'selected'; $label['class'] = 'selected';
} }
list($name) = array_values($this->__name()); list($name) = array_values($this->__name());
$select[] = sprintf($this->Html->tags['checkboxmultiple'], $name, $this->Html->_parseAttributes($htmlOptions)) . $this->label(null, $title, $label);
if(empty($attributes['class'])) {
$attributes['class'] = 'checkbox';
}
$select[] = $this->Html->div($attributes['class'], sprintf($this->Html->tags['checkboxmultiple'], $name, $this->Html->_parseAttributes($htmlOptions)) . $this->label(null, $title, $label));
} else { } else {
$select[] = sprintf($this->Html->tags['selectoption'], $name, $this->Html->_parseAttributes($htmlOptions), $title); $select[] = sprintf($this->Html->tags['selectoption'], $name, $this->Html->_parseAttributes($htmlOptions), $title);
} }

View file

@ -268,7 +268,7 @@ class FormHelperTest extends CakeTestCase {
$this->Form->params['_Token']['key'] = 'testKey'; $this->Form->params['_Token']['key'] = 'testKey';
$result = $this->Form->create('Contact', array('url' => '/contacts/add')); $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
$expected = '/^<form method="post" action="\/contacts\/add"(.+)<input type="hidden" name="data\[__Token\]\[key\]" value="testKey"(.+)<\/p>$/'; $expected = '/^<form method="post" action="\/contacts\/add"(.+)<input type="hidden" name="data\[__Token\]\[key\]" value="testKey"(.+)<\/fieldset>$/';
$this->assertPattern($expected, $result); $this->assertPattern($expected, $result);
$result = $this->Form->input('UserForm.published', array('type' => 'text')); $result = $this->Form->input('UserForm.published', array('type' => 'text'));
@ -288,7 +288,7 @@ class FormHelperTest extends CakeTestCase {
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->Form->secure($this->Form->fields); $result = $this->Form->secure($this->Form->fields);
$expected = '/<p style="display: none;"><input type="hidden" name="data\[__Token\]\[fields\]" value="'.$fieldsKey.'" id="(.+)" \/><\/p>$/'; $expected = '/<fieldset style="display:none;"><input type="hidden" name="data\[__Token\]\[fields\]" value="'.$fieldsKey.'" id="(.+)" \/><\/fieldset>$/';
$this->assertPattern($expected, $result); $this->assertPattern($expected, $result);
$result = $this->Form->fields; $result = $this->Form->fields;
@ -306,7 +306,7 @@ class FormHelperTest extends CakeTestCase {
$this->assertFalse($this->UserForm->OpenidUrl->validates()); $this->assertFalse($this->UserForm->OpenidUrl->validates());
$result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login')); $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
$this->assertPattern('/^<form\s+[^<>]+><input\s+[^<>]+\/>$/', $result); $this->assertPattern('/^<form\s+[^<>]+><fieldset\s+[^<>]+><input\s+[^<>]+\/><\/fieldset>$/', $result);
$this->assertPattern('/^<form[^<>]+id="UserFormLoginForm"[^<>]*>/', $result); $this->assertPattern('/^<form[^<>]+id="UserFormLoginForm"[^<>]*>/', $result);
$this->assertPattern('/^<form[^<>]+method="post"[^<>]*>/', $result); $this->assertPattern('/^<form[^<>]+method="post"[^<>]*>/', $result);
$this->assertPattern('/^<form[^<>]+action="\/user_forms\/login\/"[^<>]*>/', $result); $this->assertPattern('/^<form[^<>]+action="\/user_forms\/login\/"[^<>]*>/', $result);
@ -338,7 +338,7 @@ class FormHelperTest extends CakeTestCase {
$this->assertFalse($this->ValidateUser->ValidateProfile->validates()); $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
$result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add')); $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
$this->assertPattern('/^<form\s+id="[^"]+"\s+method="post"\s+action="\/validate_users\/add\/"[^>]*><input\s+[^<>]+\/>$/', $result); $this->assertPattern('/^<form\s+id="[^"]+"\s+method="post"\s+action="\/validate_users\/add\/"[^>]*><fieldset\s+[^<>]+><input\s+[^<>]+\/><\/fieldset>$/', $result);
$expected = array( $expected = array(
'ValidateUser' => array('email' => 1), 'ValidateUser' => array('email' => 1),
@ -368,7 +368,7 @@ class FormHelperTest extends CakeTestCase {
$this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates()); $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
$result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add')); $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
$this->assertPattern('/^<form\s+id="[^"]+"\s+method="post"\s+action="\/validate_users\/add\/"[^>]*><input\s+[^<>]+\/>$/', $result); $this->assertPattern('/^<form\s+id="[^"]+"\s+method="post"\s+action="\/validate_users\/add\/"[^>]*><fieldset\s+[^<>]+><input\s+[^<>]+\/><\/fieldset>$/', $result);
$expected = array( $expected = array(
'ValidateUser' => array('email' => 1), 'ValidateUser' => array('email' => 1),
@ -505,7 +505,7 @@ class FormHelperTest extends CakeTestCase {
function testSelectAsCheckbox() { function testSelectAsCheckbox() {
$result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array(0, 1), array('multiple' => 'checkbox')); $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array(0, 1), array('multiple' => 'checkbox'));
$this->assertPattern('/^(<input[^<>]+>\s*<label[^<>]*>\w+<\/label>\s*){3}$/', $result); $this->assertPattern('/^(<div[^<>]+>\<input[^<>]+>\s*<label[^<>]*>\w+<\/label><\/div>\s*){3}$/', $result);
$this->assertNoPattern('/<label[^<>]*>[^(first|second)]<\/label>/', $result); $this->assertNoPattern('/<label[^<>]*>[^(first|second)]<\/label>/', $result);
/*$this->assertPattern('/^<input type="checkbox"[^<>]+name="data\[Model\]\[multi_field\]\[\]+value="0"[^<>]+checked="checked">/', $result); /*$this->assertPattern('/^<input type="checkbox"[^<>]+name="data\[Model\]\[multi_field\]\[\]+value="0"[^<>]+checked="checked">/', $result);
@ -805,19 +805,19 @@ class FormHelperTest extends CakeTestCase {
$result = $this->Form->select('Model.multi_field', array('1' => 'first'), null, array('multiple' => 'checkbox')); $result = $this->Form->select('Model.multi_field', array('1' => 'first'), null, array('multiple' => 'checkbox'));
$this->assertPattern('/^<input[^<>]+\/><label[^<>]+>first<\/label>$/', $result); $this->assertPattern('/^<div[^<>]+><input[^<>]+\/><label[^<>]+>first<\/label><\/div>$/', $result);
$this->assertPattern('/^<input[^<>]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/><label[^<>]+>first<\/label>$/', $result); $this->assertPattern('/^<div[^<>]+><input[^<>]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/><label[^<>]+>first<\/label><\/div>$/', $result);
$this->assertPattern('/^<input[^<>]+type="checkbox"[^<>]+\/><label[^<>]+>first<\/label>$/', $result); $this->assertPattern('/^<div[^<>]+><input[^<>]+type="checkbox"[^<>]+\/><label[^<>]+>first<\/label><\/div>$/', $result);
$this->assertPattern('/^<input[^<>]+value="1"[^<>]+\/><label[^<>]+>first<\/label>$/', $result); $this->assertPattern('/^<div[^<>]+><input[^<>]+value="1"[^<>]+\/><label[^<>]+>first<\/label><\/div>$/', $result);
$this->assertNoPattern('/^<input[^<>]+[^name|type|value]=[^<>\/]*><label[^<>]+>first<\/label>$/', $result); $this->assertNoPattern('/^<div[^<>]+><input[^<>]+[^name|type|value]=[^<>\/]*><label[^<>]+>first<\/label><\/div>$/', $result);
$result = $this->Form->select('Model.multi_field', array('2' => 'second'), null, array('multiple' => 'checkbox')); $result = $this->Form->select('Model.multi_field', array('2' => 'second'), null, array('multiple' => 'checkbox'));
$this->assertPattern('/^<input[^<>]+\/><label[^<>]+>second<\/label>$/', $result); $this->assertPattern('/^<div[^<>]+><input[^<>]+\/><label[^<>]+>second<\/label><\/div>$/', $result);
$this->assertPattern('/^<input[^<>]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/><label[^<>]+>second<\/label>$/', $result); $this->assertPattern('/^<div[^<>]+><input[^<>]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/><label[^<>]+>second<\/label><\/div>$/', $result);
$this->assertPattern('/^<input[^<>]+type="checkbox"[^<>]+\/><label[^<>]+>second<\/label>$/', $result); $this->assertPattern('/^<div[^<>]+><input[^<>]+type="checkbox"[^<>]+\/><label[^<>]+>second<\/label><\/div>$/', $result);
$this->assertPattern('/^<input[^<>]+value="2"[^<>]+\/><label[^<>]+>second<\/label>$/', $result); $this->assertPattern('/^<div[^<>]+><input[^<>]+value="2"[^<>]+\/><label[^<>]+>second<\/label><\/div>$/', $result);
$this->assertNoPattern('/^<input[^<>]+[^name|type|value]=[^<>\/]*><label[^<>]+>second<\/label>$/', $result); $this->assertNoPattern('/^<div[^<>]+><input[^<>]+[^name|type|value]=[^<>\/]*><label[^<>]+>second<\/label><\/div>$/', $result);
} }
function testInputMultipleCheckboxes() { function testInputMultipleCheckboxes() {
@ -861,19 +861,19 @@ class FormHelperTest extends CakeTestCase {
$result = $this->Form->input('Model.multi_field', array('options' => array('1' => 'first'), 'multiple' => 'checkbox', 'label' => false, 'div' => false)); $result = $this->Form->input('Model.multi_field', array('options' => array('1' => 'first'), 'multiple' => 'checkbox', 'label' => false, 'div' => false));
$this->assertPattern('/^<input[^<>]+\/><label[^<>]+>first<\/label>$/', $result); $this->assertPattern('/^<div[^<>]+><input[^<>]+\/><label[^<>]+>first<\/label><\/div>$/', $result);
$this->assertPattern('/^<input[^<>]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/><label[^<>]+>first<\/label>$/', $result); $this->assertPattern('/^<div[^<>]+><input[^<>]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/><label[^<>]+>first<\/label><\/div>$/', $result);
$this->assertPattern('/^<input[^<>]+type="checkbox"[^<>]+\/><label[^<>]+>first<\/label>$/', $result); $this->assertPattern('/^<div[^<>]+><input[^<>]+type="checkbox"[^<>]+\/><label[^<>]+>first<\/label><\/div>$/', $result);
$this->assertPattern('/^<input[^<>]+value="1"[^<>]+\/><label[^<>]+>first<\/label>$/', $result); $this->assertPattern('/^<div[^<>]+><input[^<>]+value="1"[^<>]+\/><label[^<>]+>first<\/label><\/div>$/', $result);
$this->assertNoPattern('/^<input[^<>]+[^name|type|value]=[^<>\/]*><label[^<>]+>first<\/label>$/', $result); $this->assertNoPattern('/^<div[^<>]+><input[^<>]+[^name|type|value]=[^<>\/]*><label[^<>]+>first<\/label><\/div>$/', $result);
$result = $this->Form->input('Model.multi_field', array('options' => array('2' => 'second'), 'multiple' => 'checkbox', 'label' => false, 'div' => false)); $result = $this->Form->input('Model.multi_field', array('options' => array('2' => 'second'), 'multiple' => 'checkbox', 'label' => false, 'div' => false));
$this->assertPattern('/^<input[^<>]+\/><label[^<>]+>second<\/label>$/', $result); $this->assertPattern('/^<div[^<>]+><input[^<>]+\/><label[^<>]+>second<\/label><\/div>$/', $result);
$this->assertPattern('/^<input[^<>]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/><label[^<>]+>second<\/label>$/', $result); $this->assertPattern('/^<div[^<>]+><input[^<>]+name="data\[Model\]\[multi_field\]\[\]"[^<>]+\/><label[^<>]+>second<\/label><\/div>$/', $result);
$this->assertPattern('/^<input[^<>]+type="checkbox"[^<>]+\/><label[^<>]+>second<\/label>$/', $result); $this->assertPattern('/^<div[^<>]+><input[^<>]+type="checkbox"[^<>]+\/><label[^<>]+>second<\/label><\/div>$/', $result);
$this->assertPattern('/^<input[^<>]+value="2"[^<>]+\/><label[^<>]+>second<\/label>$/', $result); $this->assertPattern('/^<div[^<>]+><input[^<>]+value="2"[^<>]+\/><label[^<>]+>second<\/label><\/div>$/', $result);
$this->assertNoPattern('/^<input[^<>]+[^name|type|value]=[^<>\/]*><label[^<>]+>second<\/label>$/', $result); $this->assertNoPattern('/^<div[^<>]+><input[^<>]+[^name|type|value]=[^<>\/]*><label[^<>]+>second<\/label><\/div>$/', $result);
} }
function testCheckbox() { function testCheckbox() {
@ -1241,7 +1241,7 @@ class FormHelperTest extends CakeTestCase {
function testFormMagicInput() { function testFormMagicInput() {
$result = $this->Form->create('Contact'); $result = $this->Form->create('Contact');
$this->assertPattern('/^<form\s+id="ContactAddForm"\s+method="post"\s+action="\/contacts\/add\/"\s*><input\s+[^<>]+\/>$/', $result); $this->assertPattern('/^<form\s+id="ContactAddForm"\s+method="post"\s+action="\/contacts\/add\/"\s*><fieldset[^<>]+><input\s+[^<>]+\/><\/fieldset>$/', $result);
$this->assertNoPattern('/^<form[^<>]+[^id|method|action]=[^<>]*>/', $result); $this->assertNoPattern('/^<form[^<>]+[^id|method|action]=[^<>]*>/', $result);
$result = $this->Form->input('name'); $result = $this->Form->input('name');
@ -1294,7 +1294,7 @@ class FormHelperTest extends CakeTestCase {
function testForMagicInputNonExistingNorValidated() { function testForMagicInputNonExistingNorValidated() {
$result = $this->Form->create('Contact'); $result = $this->Form->create('Contact');
$this->assertPattern('/^<form\s+id="ContactAddForm"\s+method="post"\s+action="\/contacts\/add\/"\s*><input\s+[^<>]+\/>$/', $result); $this->assertPattern('/^<form\s+id="ContactAddForm"\s+method="post"\s+action="\/contacts\/add\/"\s*><fieldset[^<>]+><input\s+[^<>]+\/><\/fieldset>$/', $result);
$this->assertNoPattern('/^<form[^<>]+[^id|method|action]=[^<>]*>/', $result); $this->assertNoPattern('/^<form[^<>]+[^id|method|action]=[^<>]*>/', $result);
$result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false)); $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
@ -1328,7 +1328,7 @@ class FormHelperTest extends CakeTestCase {
function testFormMagicInputLabel() { function testFormMagicInputLabel() {
$result = $this->Form->create('Contact'); $result = $this->Form->create('Contact');
$this->assertPattern('/^<form\s+id="ContactAddForm"\s+method="post"\s+action="\/contacts\/add\/"\s*><input\s+[^<>]+\/>$/', $result); $this->assertPattern('/^<form\s+id="ContactAddForm"\s+method="post"\s+action="\/contacts\/add\/"\s*><fieldset[^<>]+><input\s+[^<>]+\/><\/fieldset>$/', $result);
$result = $this->Form->input('Contact.name', array('div' => false, 'label' => false)); $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); $this->assertPattern('/^<input name="data\[Contact\]\[name\]" type="text" maxlength="255" value="" id="ContactName" \/>$/', $result);