Closes #2781, $form->input on date fields renders maxYear and minYear in $options as HTML attributes.

Closes #3181, Year list ordering in FormHelper order is now formated descending.
Fixes #3524, FormHelper->month, day, year, hour, and minute should use $selected if it is supplied.
Fixes #3558, post validation don't pass if form use only hidden fields.
Fixes #3568, Saving datetime fields is broken in scaffolding.
Fixes #3574, Typo in Session class.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5987 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2007-11-12 11:31:43 +00:00
parent c7307742e2
commit eb6c94a01e
4 changed files with 154 additions and 101 deletions

View file

@ -136,7 +136,7 @@ class CakeSession extends Object {
if ($start === true) {
$this->host = env('HTTP_HOST');
if (empty($base) || strpos($base, '?' || strpos($base, 'index.php'))) {
if (empty($base) || strpos($base, '?') === 0 || strpos($base, 'index.php') === 0) {
$this->path = '/';
} else {
$this->path = $base;

View file

@ -369,7 +369,7 @@ class Helper extends Overloadable {
if ($view->modelScope === false) {
list($view->model, $view->field) = $parts;
} elseif ($sameScope === true && $hasField === 0) {
list($view->field, $view->suffix) = $parts;
list($view->field, $view->fieldSuffix) = $parts;
} elseif ($sameScope === true && $hasField === 1) {
list($view->modelId, $view->field) = $parts;
} else {

View file

@ -275,6 +275,11 @@ class FormHelper extends AppHelper {
foreach ($fields as $key => $value) {
if (strpos($key, '_') !== 0) {
sort($fields[$key]);
} else {
$model = substr($key, 1);
if ($key !== '__Token' && !isset($fields[$model])) {
$fields[$model] = array();
}
}
}
ksort($fields);
@ -1036,21 +1041,26 @@ class FormHelper extends AppHelper {
* @return string
*/
function day($fieldName, $selected = null, $attributes = array(), $showEmpty = true) {
$value = $this->value($fieldName);
if (empty($selected) && $value = $this->value($fieldName)) {
if (is_array($value)) {
extract($value);
$selected = $day;
} else {
if (empty($value)) {
if (!$showEmpty && !$selected) {
$value = 'now';
} elseif (strlen($selected) > 2) {
$value = $selected;
if (!$showEmpty) {
$selected = 'now';
}
} else {
$selected = $value;
}
}
}
if (strlen($selected) > 2) {
$selected = date('d', strtotime($selected));
} elseif ($selected === false) {
$selected = null;
}
}
if (!empty($value)) {
$selected = date('d', strtotime($value));
}
return $this->select($fieldName . ".day", $this->__generateOptions('day'), $selected, $attributes, $showEmpty);
}
/**
@ -1065,22 +1075,28 @@ class FormHelper extends AppHelper {
* @return string
*/
function year($fieldName, $minYear = null, $maxYear = null, $selected = null, $attributes = array(), $showEmpty = true) {
$value = $this->value($fieldName);
if (empty($selected) && $value = $this->value($fieldName)) {
if (is_array($value)) {
extract($value);
$selected = $year;
} else {
if (empty($value)) {
if (!$showEmpty && !$maxYear && !$selected) {
$value = 'now';
if (!$showEmpty && !$maxYear) {
$selected = 'now';
} elseif (!$showEmpty && $maxYear && !$selected) {
$selected = $maxYear;
} elseif (strlen($selected) > 4) {
$value = $selected;
} elseif ($selected === false) {
$selected = null;
}
} else {
$selected = $value;
}
}
}
if (!empty($value)) {
$selected = date('Y', strtotime($value));
if (strlen($selected) > 4 || $selected === 'now') {
$selected = date('Y', strtotime($selected));
} elseif ($selected === false) {
$selected = null;
}
return $this->select($fieldName . ".year", $this->__generateOptions('year', $minYear, $maxYear), $selected, $attributes, $showEmpty);
}
@ -1093,21 +1109,26 @@ class FormHelper extends AppHelper {
* @return string
*/
function month($fieldName, $selected = null, $attributes = array(), $showEmpty = true) {
$value = $this->value($fieldName);
if (empty($selected) && $value = $this->value($fieldName)) {
if (is_array($value)) {
extract($value);
$selected = $month;
} else {
if (empty($value)) {
if (!$showEmpty && !$selected) {
$value = 'now';
} elseif (strlen($selected) > 2) {
$value = $selected;
if (!$showEmpty) {
$selected = 'now';
}
} else {
$selected = $value;
}
}
}
if (strlen($selected) > 2) {
$selected = date('m', strtotime($selected));
} elseif ($selected === false) {
$selected = null;
}
}
if (!empty($value)) {
$selected = date('m', strtotime($value));
}
return $this->select($fieldName . ".month", $this->__generateOptions('month'), $selected, $attributes, $showEmpty);
}
/**
@ -1121,23 +1142,30 @@ class FormHelper extends AppHelper {
* @return string
*/
function hour($fieldName, $format24Hours = false, $selected = null, $attributes = array(), $showEmpty = true) {
$value = $this->value($fieldName);
if (empty($selected) && $value = $this->value($fieldName)) {
if (is_array($value)) {
extract($value);
$selected = $hour;
} else {
if (empty($value)) {
if (!$showEmpty && !$selected) {
$value = 'now';
} elseif (strlen($selected) > 2) {
$value = $selected;
if (!$showEmpty) {
$selected = 'now';
}
} else {
$selected = $value;
}
}
}
if (strlen($selected) > 2) {
if ($format24Hours) {
$selected = date('H', strtotime($value));
} else {
$selected = date('g', strtotime($value));
}
} elseif ($selected === false) {
$selected = null;
}
}
if (!empty($value) && $format24Hours) {
$selected = date('H', strtotime($value));
} elseif (!empty($value) && !$format24Hours) {
$selected = date('g', strtotime($value));
}
return $this->select($fieldName . ".hour", $this->__generateOptions($format24Hours ? 'hour24' : 'hour'), $selected, $attributes, $showEmpty);
}
/**
@ -1148,21 +1176,26 @@ class FormHelper extends AppHelper {
* @return string
*/
function minute($fieldName, $selected = null, $attributes = array(), $showEmpty = true) {
$value = $this->value($fieldName);
if (empty($selected) && $value = $this->value($fieldName)) {
if (is_array($value)) {
extract($value);
$selected = $min;
} else {
if (empty($value)) {
if (!$showEmpty && !$selected) {
$value = 'now';
} elseif (strlen($selected) > 2) {
$value = $selected;
if (!$showEmpty) {
$selected = 'now';
}
} else {
$selected = $value;
}
}
}
if (strlen($selected) > 2) {
$selected = date('i', strtotime($selected));
} elseif ($selected === false) {
$selected = null;
}
}
if (!empty($value)) {
$selected = date('i', strtotime($value));
}
return $this->select($fieldName . ".min", $this->__generateOptions('minute'), $selected, $attributes, $showEmpty);
}
/**
@ -1174,9 +1207,23 @@ class FormHelper extends AppHelper {
*/
function meridian($fieldName, $selected = null, $attributes = array(), $showEmpty = true) {
if (empty($selected) && $value = $this->value($fieldName)) {
if (is_array($value)) {
extract($value);
$selected = $meridian;
} else {
if (empty($value)) {
if (!$showEmpty) {
$selected = date('a');
}
} else {
$selected = date('a', strtotime($value));
}
$selected = empty($selected) ? ($showEmpty ? null : date('a')) : $selected;
}
}
if ($selected === false) {
$selected = null;
}
return $this->select($fieldName . ".meridian", $this->__generateOptions('meridian'), $selected, $attributes, $showEmpty);
}
/**
@ -1201,11 +1248,12 @@ class FormHelper extends AppHelper {
}
if (!empty($selected)) {
if (is_array($selected)) {
extract($selected);
} else {
if (is_int($selected)) {
$selected = strftime('%Y-%m-%d %H:%M:%S', $selected);
}
$meridian = 'am';
$pos = strpos($selected, '-');
if ($pos !== false) {
@ -1228,13 +1276,18 @@ class FormHelper extends AppHelper {
} elseif ($time[0] > 12) {
$meridian = 'pm';
}
$hour = $time[0];
$min = $time[1];
}
}
}
$elements = array('Day','Month','Year','Hour','Minute','Meridian');
$attributes = am(array('minYear' => null, 'maxYear' => null, 'separator' => '-'), $attributes);
$minYear = $attributes['minYear'];
$maxYear = $attributes['maxYear'];
$separator = $attributes['separator'];
unset($attributes['minYear'], $attributes['maxYear'], $attributes['separator']);
if (isset($attributes['id'])) {
if (is_string($attributes['id'])) {
// build out an array version
@ -1258,7 +1311,6 @@ class FormHelper extends AppHelper {
}
}
$attributes = am(array('minYear' => null, 'maxYear' => null, 'separator' => '-'), $attributes);
$opt = '';
if ($dateFormat != 'NONE') {
@ -1266,7 +1318,7 @@ class FormHelper extends AppHelper {
foreach (preg_split('//', $dateFormat, -1, PREG_SPLIT_NO_EMPTY) as $char) {
switch ($char) {
case 'Y':
$selects[] = $this->year($fieldName, $attributes['minYear'], $attributes['maxYear'], $year, $selectYearAttr, $showEmpty);
$selects[] = $this->year($fieldName, $minYear, $maxYear, $year, $selectYearAttr, $showEmpty);
break;
case 'M':
$selects[] = $this->month($fieldName, $month, $selectMonthAttr, $showEmpty);
@ -1276,7 +1328,7 @@ class FormHelper extends AppHelper {
break;
}
}
$opt = implode($attributes['separator'], $selects);
$opt = implode($separator, $selects);
}
switch($timeFormat) {
@ -1432,6 +1484,7 @@ class FormHelper extends AppHelper {
for ($i = $min; $i <= $max; $i++) {
$data[$i] = $i;
}
$data = array_reverse($data, true);
break;
}
$this->__options[$name] = $data;

View file

@ -920,42 +920,42 @@ class FormHelperTest extends CakeTestCase {
$this->data['Contact']['published'] = '';
$result = $this->Form->year('Contact.published', 2006, 2007, null, array('class' => 'year'));
$expecting = "<select name=\"data[Contact][published][year]\" class=\"year\" id=\"ContactPublishedYear\">\n<option value=\"\"></option>\n<option value=\"2006\">2006</option>\n<option value=\"2007\">2007</option>\n</select>";
$expecting = "<select name=\"data[Contact][published][year]\" class=\"year\" id=\"ContactPublishedYear\">\n<option value=\"\"></option>\n<option value=\"2007\">2007</option>\n<option value=\"2006\">2006</option>\n</select>";
$this->assertEqual($result, $expecting);
$this->Form->data['Contact']['published'] = '2006-10-10';
$result = $this->Form->year('Contact.published', 2006, 2007, null, array(), false);
$expecting = "<select name=\"data[Contact][published][year]\" id=\"ContactPublishedYear\">\n<option value=\"2006\" selected=\"selected\">2006</option>\n<option value=\"2007\">2007</option>\n</select>";
$expecting = "<select name=\"data[Contact][published][year]\" id=\"ContactPublishedYear\">\n<option value=\"2007\">2007</option>\n<option value=\"2006\" selected=\"selected\">2006</option>\n</select>";
$this->assertEqual($result, $expecting);
$this->Form->data['Contact']['published'] = '';
$result = $this->Form->year('Contact.published', 2006, 2007, false);
$expecting = "<select name=\"data[Contact][published][year]\" id=\"ContactPublishedYear\">\n<option value=\"\"></option>\n<option value=\"2006\">2006</option>\n<option value=\"2007\">2007</option>\n</select>";
$expecting = "<select name=\"data[Contact][published][year]\" id=\"ContactPublishedYear\">\n<option value=\"\"></option>\n<option value=\"2007\">2007</option>\n<option value=\"2006\">2006</option>\n</select>";
$this->assertEqual($result, $expecting);
$this->Form->data['Contact']['published'] = '2006-10-10';
$result = $this->Form->year('Contact.published', 2006, 2007, false, array(), false);
$expecting = "<select name=\"data[Contact][published][year]\" id=\"ContactPublishedYear\">\n<option value=\"2006\" selected=\"selected\">2006</option>\n<option value=\"2007\">2007</option>\n</select>";
$expecting = "<select name=\"data[Contact][published][year]\" id=\"ContactPublishedYear\">\n<option value=\"2007\">2007</option>\n<option value=\"2006\" selected=\"selected\">2006</option>\n</select>";
$this->assertEqual($result, $expecting);
$this->Form->data['Contact']['published'] = '';
$result = $this->Form->year('Contact.published', 2006, 2007, 2007);
$expecting = "<select name=\"data[Contact][published][year]\" id=\"ContactPublishedYear\">\n<option value=\"\"></option>\n<option value=\"2006\">2006</option>\n<option value=\"2007\" selected=\"selected\">2007</option>\n</select>";
$expecting = "<select name=\"data[Contact][published][year]\" id=\"ContactPublishedYear\">\n<option value=\"\"></option>\n<option value=\"2007\" selected=\"selected\">2007</option>\n<option value=\"2006\">2006</option>\n</select>";
$this->assertEqual($result, $expecting);
$this->Form->data['Contact']['published'] = '2006-10-10';
$result = $this->Form->year('Contact.published', 2006, 2007, 2007, array(), false);
$expecting = "<select name=\"data[Contact][published][year]\" id=\"ContactPublishedYear\">\n<option value=\"2006\" selected=\"selected\">2006</option>\n<option value=\"2007\">2007</option>\n</select>";
$expecting = "<select name=\"data[Contact][published][year]\" id=\"ContactPublishedYear\">\n<option value=\"2007\" selected=\"selected\">2007</option>\n<option value=\"2006\">2006</option>\n</select>";
$this->assertEqual($result, $expecting);
$this->Form->data['Contact']['published'] = '';
$result = $this->Form->year('Contact.published', 2006, 2008, 2007, array(), false);
$expecting = "<select name=\"data[Contact][published][year]\" id=\"ContactPublishedYear\">\n<option value=\"2006\">2006</option>\n<option value=\"2007\" selected=\"selected\">2007</option>\n<option value=\"2008\">2008</option>\n</select>";
$expecting = "<select name=\"data[Contact][published][year]\" id=\"ContactPublishedYear\">\n<option value=\"2008\">2008</option>\n<option value=\"2007\" selected=\"selected\">2007</option>\n<option value=\"2006\">2006</option>\n</select>";
$this->assertEqual($result, $expecting);
$this->Form->data['Contact']['published'] = '2006-10-10';
$result = $this->Form->year('Contact.published', 2006, 2008, null, array(), false);
$expecting = "<select name=\"data[Contact][published][year]\" id=\"ContactPublishedYear\">\n<option value=\"2006\" selected=\"selected\">2006</option>\n<option value=\"2007\">2007</option>\n<option value=\"2008\">2008</option>\n</select>";
$expecting = "<select name=\"data[Contact][published][year]\" id=\"ContactPublishedYear\">\n<option value=\"2008\">2008</option>\n<option value=\"2007\">2007</option>\n<option value=\"2006\" selected=\"selected\">2006</option>\n</select>";
$this->assertEqual($result, $expecting);
}