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) { if ($start === true) {
$this->host = env('HTTP_HOST'); $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 = '/'; $this->path = '/';
} else { } else {
$this->path = $base; $this->path = $base;

View file

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

View file

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

View file

@ -920,42 +920,42 @@ class FormHelperTest extends CakeTestCase {
$this->data['Contact']['published'] = ''; $this->data['Contact']['published'] = '';
$result = $this->Form->year('Contact.published', 2006, 2007, null, array('class' => 'year')); $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->assertEqual($result, $expecting);
$this->Form->data['Contact']['published'] = '2006-10-10'; $this->Form->data['Contact']['published'] = '2006-10-10';
$result = $this->Form->year('Contact.published', 2006, 2007, null, array(), false); $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->assertEqual($result, $expecting);
$this->Form->data['Contact']['published'] = ''; $this->Form->data['Contact']['published'] = '';
$result = $this->Form->year('Contact.published', 2006, 2007, false); $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->assertEqual($result, $expecting);
$this->Form->data['Contact']['published'] = '2006-10-10'; $this->Form->data['Contact']['published'] = '2006-10-10';
$result = $this->Form->year('Contact.published', 2006, 2007, false, array(), false); $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->assertEqual($result, $expecting);
$this->Form->data['Contact']['published'] = ''; $this->Form->data['Contact']['published'] = '';
$result = $this->Form->year('Contact.published', 2006, 2007, 2007); $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->assertEqual($result, $expecting);
$this->Form->data['Contact']['published'] = '2006-10-10'; $this->Form->data['Contact']['published'] = '2006-10-10';
$result = $this->Form->year('Contact.published', 2006, 2007, 2007, array(), false); $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->assertEqual($result, $expecting);
$this->Form->data['Contact']['published'] = ''; $this->Form->data['Contact']['published'] = '';
$result = $this->Form->year('Contact.published', 2006, 2008, 2007, array(), false); $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->assertEqual($result, $expecting);
$this->Form->data['Contact']['published'] = '2006-10-10'; $this->Form->data['Contact']['published'] = '2006-10-10';
$result = $this->Form->year('Contact.published', 2006, 2008, null, array(), false); $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); $this->assertEqual($result, $expecting);
} }