Skip to content

Commit

Permalink
Improve error handling of invalid values provided to the DatePicker F…
Browse files Browse the repository at this point in the history
…ormWidget
  • Loading branch information
LukeTowers committed May 23, 2022
1 parent c3c0279 commit 734cfe8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
13 changes: 12 additions & 1 deletion formwidgets/DatePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Backend\Classes\FormField;
use Backend\Classes\FormWidgetBase;
use System\Helpers\DateTime as DateTimeHelper;
use Winter\Storm\Exception\ApplicationException;

/**
* Date picker
Expand Down Expand Up @@ -108,7 +109,12 @@ public function init()
*/
public function render()
{
$this->prepareVars();
try {
$this->prepareVars();
} catch (ApplicationException $ex) {
$this->vars['error'] = $ex->getMessage();
}

return $this->makePartial('datepicker');
}

Expand All @@ -119,6 +125,11 @@ public function prepareVars()
{
if ($value = $this->getLoadValue()) {
$value = DateTimeHelper::makeCarbon($value, false);

if (!($value instanceof Carbon)) {
throw new ApplicationException(sprintf('"%s" is not a valid date / time value.', $value));
}

if ($this->mode === 'date' && !$this->ignoreTimezone) {
$backendTimeZone = \Backend\Models\Preference::get('timezone');
$value->setTimezone($backendTimeZone);
Expand Down
6 changes: 6 additions & 0 deletions formwidgets/datepicker/partials/_datepicker.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<?php if (!empty($error)) : ?>
<p class="flash-message static error">
<?= e($error); ?></p>
</p>
<?php return; ?>
<?php endif; ?>
<?php if ($this->previewMode): ?>
<div class="form-control"><?= Backend::dateTime($value, [
'format' => $format,
Expand Down

0 comments on commit 734cfe8

Please sign in to comment.