fixing form helper for tickets #2726 and #2659, updated tests.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5258 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-06-08 01:17:40 +00:00
parent 02508cfb02
commit 851a834995
4 changed files with 72 additions and 27 deletions

View file

@ -27,7 +27,7 @@
/** /**
* Include files * Include files
*/ */
uses('controller' . DS . 'component', 'view' . DS . 'view'); uses('controller' . DS . 'component', 'view' . DS . 'view');
/** /**
* Controller * Controller
* *
@ -241,9 +241,9 @@ class Controller extends Object {
* action names and values that denote cache expiration times (in seconds). * action names and values that denote cache expiration times (in seconds).
* *
* Example: var $cacheAction = array( * Example: var $cacheAction = array(
'view/23/' => 21600, * 'view/23/' => 21600,
'recalled/' => 86400 * 'recalled/' => 86400
); * );
* *
* $cacheAction can also be set to a strtotime() compatible string. This * $cacheAction can also be set to a strtotime() compatible string. This
* marks all the actions in the controller for view caching. * marks all the actions in the controller for view caching.

View file

@ -304,21 +304,36 @@ class Helper extends Overloadable {
*/ */
function setFormTag($tagValue) { function setFormTag($tagValue) {
$view =& ClassRegistry::getObject('view'); $view =& ClassRegistry::getObject('view');
$parts = preg_split('/\/|\./', $tagValue);
if($tagValue === null) {
$view->model = null;
$view->association = null;
$view->modelId = null;
return;
}
$parts = preg_split('/\/|\./', $tagValue);
$view->association = null;
if (count($parts) == 1) { if (count($parts) == 1) {
$view->field = $parts[0]; $view->field = $parts[0];
} elseif (count($parts) == 2 && is_numeric($parts[0])) { } elseif (count($parts) == 2 && is_numeric($parts[0])) {
$view->modelId = $parts[0]; $view->modelId = $parts[0];
$view->field = $parts[1]; $view->field = $parts[1];
} elseif (count($parts) == 2) { } elseif (count($parts) == 2 && empty($parts[1])) {
$view->model = $parts[0]; $view->model = $parts[0];
$view->field = $parts[1]; $view->field = $parts[1];
} elseif (count($parts) == 2) {
$view->association = $parts[0];
$view->field = $parts[1];
} elseif (count($parts) == 3) { } elseif (count($parts) == 3) {
$view->model = $parts[0]; $view->association = $parts[0];
$view->modelId = $parts[1]; $view->modelId = $parts[1];
$view->field = $parts[2]; $view->field = $parts[2];
} }
if(!isset($view->model)) {
$view->model = $view->association;
$view->association = null;
}
} }
/** /**
* Gets the currently-used model of the rendering context. * Gets the currently-used model of the rendering context.
@ -327,7 +342,11 @@ class Helper extends Overloadable {
*/ */
function model() { function model() {
$view =& ClassRegistry::getObject('view'); $view =& ClassRegistry::getObject('view');
if($view->association == null) {
return $view->model; return $view->model;
} else {
return $view->association;
}
} }
/** /**
* Gets the ID of the currently-used model of the rendering context. * Gets the ID of the currently-used model of the rendering context.

View file

@ -47,7 +47,7 @@ class FormHelper extends AppHelper {
* *
* @access public * @access public
*/ */
var $fieldset = array('fields'=>array(), 'sizes'=>array(), 'key'=>'id', 'validates'=>array()); var $fieldset = array('fields'=> array(), 'sizes'=> array(), 'key'=> 'id', 'validates'=> array());
/** /**
* Enter description here... * Enter description here...
* *
@ -71,7 +71,7 @@ class FormHelper extends AppHelper {
*/ */
function create($model = null, $options = array()) { function create($model = null, $options = array()) {
$defaultModel = null; $defaultModel = null;
$data = array('fields' => '','key' => '', 'validates' => ''); $data = $this->fieldset;
$view =& ClassRegistry::getObject('view'); $view =& ClassRegistry::getObject('view');
if (is_array($model) && empty($options)) { if (is_array($model) && empty($options)) {
@ -124,6 +124,12 @@ class FormHelper extends AppHelper {
'key' => $object->primaryKey, 'key' => $object->primaryKey,
'validates' => (ife(empty($object->validate), array(), array_keys($object->validate))) 'validates' => (ife(empty($object->validate), array(), array_keys($object->validate)))
); );
$habtm = array();
if(!empty($object->hasAndBelongsToMany)) {
$habtm = array_combine(array_keys($object->hasAndBelongsToMany), array_keys($object->hasAndBelongsToMany));
}
$data['fields'] = am($habtm, $data['fields']);
$this->fieldset = $data; $this->fieldset = $data;
} }
@ -201,7 +207,6 @@ class FormHelper extends AppHelper {
$append .= '</p>'; $append .= '</p>';
} }
$this->setFormTag($model . '.');
return $this->output(sprintf($this->Html->tags['form'], $this->Html->_parseAttributes($htmlAttributes, null, ''))) . $append; return $this->output(sprintf($this->Html->tags['form'], $this->Html->_parseAttributes($htmlAttributes, null, ''))) . $append;
} }
/** /**
@ -245,6 +250,7 @@ class FormHelper extends AppHelper {
$out .= $this->secure($this->fields); $out .= $this->secure($this->fields);
$this->fields = array(); $this->fields = array();
} }
$this->setFormTag(null);
$out .= $this->Html->tags['formend']; $out .= $this->Html->tags['formend'];
return $this->output($out); return $this->output($out);
} }
@ -481,7 +487,20 @@ class FormHelper extends AppHelper {
if($this->field() == $primaryKey) { if($this->field() == $primaryKey) {
$options['type'] = 'hidden'; $options['type'] = 'hidden';
} }
}
}
if($options['type'] == 'select') {
$multiple = false;
if(in_array($this->field(), array_values($this->fieldset['fields']))) {
$multiple = true;
} else if (isset($model) && $model->name == $this->model()){
if(isset($model) && in_array($this->field(), array_keys($model->hasAndBelongsToMany))) {
$multiple = true;
}
}
if(!isset($options['multiple']) && $multiple) {
$options['multiple'] = 'multiple';
} }
} }
@ -921,7 +940,10 @@ class FormHelper extends AppHelper {
*/ */
function year($fieldName, $minYear = null, $maxYear = null, $selected = null, $attributes = array(), $showEmpty = true) { function year($fieldName, $minYear = null, $maxYear = null, $selected = null, $attributes = array(), $showEmpty = true) {
$value = $this->value($fieldName); $value = $this->value($fieldName);
if (empty($value)) { if(!empty($value)) {
$selected = date('Y', strtotime($value));
}
if (empty($selected)) {
if(!$showEmpty && !$maxYear) { if(!$showEmpty && !$maxYear) {
$value = 'now'; $value = 'now';
} else if(!$showEmpty && $maxYear) { } else if(!$showEmpty && $maxYear) {
@ -932,9 +954,7 @@ class FormHelper extends AppHelper {
$selected = null; $selected = null;
} }
} }
if(!empty($value)) {
$selected = date('Y', strtotime($value));
}
return $this->select($fieldName . "_year", $this->__generateOptions('year', $minYear, $maxYear), $selected, $attributes, $showEmpty); return $this->select($fieldName . "_year", $this->__generateOptions('year', $minYear, $maxYear), $selected, $attributes, $showEmpty);
} }
/** /**

View file

@ -422,19 +422,19 @@ class FormHelperTest extends CakeTestCase {
$result = $this->Form->label(); $result = $this->Form->label();
$this->assertEqual($result, '<label for="PersonName">Name</label>'); $this->assertEqual($result, '<label for="PersonName">Name</label>');
$result = $this->Form->label('first_name'); $result = $this->Form->label('Person.first_name');
$this->assertEqual($result, '<label for="PersonFirstName">First Name</label>'); $this->assertEqual($result, '<label for="PersonFirstName">First Name</label>');
$result = $this->Form->label('first_name', 'Your first name'); $result = $this->Form->label('Person.first_name', 'Your first name');
$this->assertEqual($result, '<label for="PersonFirstName">Your first name</label>'); $this->assertEqual($result, '<label for="PersonFirstName">Your first name</label>');
$result = $this->Form->label('first_name', 'Your first name', array('class' => 'my-class')); $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
$this->assertEqual($result, '<label for="PersonFirstName" class="my-class">Your first name</label>'); $this->assertEqual($result, '<label for="PersonFirstName" class="my-class">Your first name</label>');
$result = $this->Form->label('first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID')); $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
$this->assertEqual($result, '<label for="PersonFirstName" class="my-class" id="LabelID">Your first name</label>'); $this->assertEqual($result, '<label for="PersonFirstName" class="my-class" id="LabelID">Your first name</label>');
$result = $this->Form->label('first_name', ''); $result = $this->Form->label('Person.first_name', '');
$this->assertEqual($result, '<label for="PersonFirstName"></label>'); $this->assertEqual($result, '<label for="PersonFirstName"></label>');
} }
@ -620,6 +620,7 @@ class FormHelperTest extends CakeTestCase {
} }
function testYear() { function testYear() {
$result = $this->Form->year('Model.field', 2006, 2007); $result = $this->Form->year('Model.field', 2006, 2007);
$this->assertPattern('/option value="2006"/', $result); $this->assertPattern('/option value="2006"/', $result);
$this->assertPattern('/option value="2007"/', $result); $this->assertPattern('/option value="2007"/', $result);
@ -647,18 +648,18 @@ class FormHelperTest extends CakeTestCase {
$this->assertEqual($result, $expecting); $this->assertEqual($result, $expecting);
$this->Form->data['Model']['field'] = ''; $this->Form->data['Model']['field'] = '';
$result = $this->Form->year('Model.field', 2006, 2007, '2007'); $result = $this->Form->year('Model.field', 2006, 2007, 2007);
$expecting = "<select name=\"data[Model][field_year]\" id=\"ModelFieldYear\">\n<option value=\"\"></option>\n<option value=\"2006\">2006</option>\n<option value=\"2007\" selected=\"selected\">2007</option>\n</select>"; $expecting = "<select name=\"data[Model][field_year]\" id=\"ModelFieldYear\">\n<option value=\"\"></option>\n<option value=\"2006\">2006</option>\n<option value=\"2007\" selected=\"selected\">2007</option>\n</select>";
$this->assertEqual($result, $expecting); $this->assertEqual($result, $expecting);
$this->Form->data['Model']['field'] = '2006-10-10'; $this->Form->data['Model']['field'] = '2006-10-10';
$result = $this->Form->year('Model.field', 2006, 2007, '2007', array(), false); $result = $this->Form->year('Model.field', 2006, 2007, 2007, array(), false);
$expecting = "<select name=\"data[Model][field_year]\" id=\"ModelFieldYear\">\n<option value=\"2006\" selected=\"selected\">2006</option>\n<option value=\"2007\">2007</option>\n</select>"; $expecting = "<select name=\"data[Model][field_year]\" id=\"ModelFieldYear\">\n<option value=\"2006\" selected=\"selected\">2006</option>\n<option value=\"2007\">2007</option>\n</select>";
$this->assertEqual($result, $expecting); $this->assertEqual($result, $expecting);
$this->Form->data['Model']['field'] = ''; $this->Form->data['Model']['field'] = '';
$result = $this->Form->year('Model.field', 2006, 2008, null, array(), false); $result = $this->Form->year('Model.field', 2006, 2008, 2007, array(), false);
$expecting = "<select name=\"data[Model][field_year]\" id=\"ModelFieldYear\">\n<option value=\"2006\">2006</option>\n<option value=\"2007\">2007</option>\n<option value=\"2008\" selected=\"selected\">2008</option>\n</select>"; $expecting = "<select name=\"data[Model][field_year]\" id=\"ModelFieldYear\">\n<option value=\"2006\">2006</option>\n<option value=\"2007\" selected=\"selected\">2007</option>\n<option value=\"2008\">2008</option>\n</select>";
$this->assertEqual($result, $expecting); $this->assertEqual($result, $expecting);
$this->Form->data['Model']['field'] = '2006-10-10'; $this->Form->data['Model']['field'] = '2006-10-10';
@ -750,13 +751,18 @@ class FormHelperTest extends CakeTestCase {
$result = $this->Form->create('Contact'); $result = $this->Form->create('Contact');
$this->assertPattern('/^<form\s+id="ContactAddForm"\s+method="post"\s+action="\/contacts\/add\/"\s*>$/', $result); $this->assertPattern('/^<form\s+id="ContactAddForm"\s+method="post"\s+action="\/contacts\/add\/"\s*>$/', $result);
$result = $this->Form->input('Contact.name'); $result = $this->Form->input('name');
$this->assertPattern('/^<div class="input">' . $this->assertPattern('/^<div class="input">' .
'<label for="ContactName">Name<\/label>' . '<label for="ContactName">Name<\/label>' .
'<input name="data\[Contact\]\[name\]" type="text" maxlength="255" value="" id="ContactName" \/>'. '<input name="data\[Contact\]\[name\]" type="text" maxlength="255" value="" id="ContactName" \/>'.
'<\/div>$/', $result); '<\/div>$/', $result);
$result = $this->Form->input('Address.street');
$this->assertPattern('/^<div class="input">' .
'<label for="AddressStreet">Street<\/label>' .
'<input name="data\[Address\]\[street\]" type="text" value="" id="AddressStreet" \/>'.
'<\/div>$/', $result);
$result = $this->Form->input('Contact.name', array('div' => false)); $result = $this->Form->input('name', array('div' => false));
$this->assertPattern('/^<label for="ContactName">Name<\/label>' . $this->assertPattern('/^<label for="ContactName">Name<\/label>' .
'<input name="data\[Contact\]\[name\]" type="text" maxlength="255" value="" id="ContactName" \/>$/', $result); '<input name="data\[Contact\]\[name\]" type="text" maxlength="255" value="" id="ContactName" \/>$/', $result);