Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Core/Assets/JS/WidgetDatetime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This file is part of FacturaScripts
* Copyright (C) 2026 Carlos Garcia Gomez <carlos@facturascripts.com>
*
* Completa y valida los campos datetime del formulario.
*
* En navegadores como Safari el input datetime-local se comporta como un campo
* de texto: el usuario puede enviar el formulario con la hora sin rellenar, o
* incluso con una fecha incompleta. Este script, al cambiar el valor:
* 1. Rellena automáticamente la hora y los minutos con la hora actual si faltan.
* 2. Avisa (y limpia el campo) si falta el día, el mes o el año.
*
* Se usa delegación de eventos sobre document para cubrir también las filas
* añadidas dinámicamente (EditListView).
*/
document.addEventListener('change', function (event) {
const input = event.target;
if (input && input.matches && input.matches('input.widget-datetime')) {
widgetDatetimeComplete(input);
}
});

function widgetDatetimeComplete(input) {
let value = (input.value || '').trim();
if (value === '') {
return;
}

// separa fecha y hora aceptando como separador la "T" o un espacio
const parts = value.split(/[T ]/);
const datePart = parts[0];
let timePart = parts.length > 1 ? parts[1] : '';

// la fecha debe tener año, mes y día (formato YYYY-MM-DD)
if (/^\d{4}-\d{2}-\d{2}$/.test(datePart) === false) {
window.alert('Fecha incompleta. Indica día, mes y año.');
input.value = '';
input.focus();
return;
}

// completa la hora con la actual si falta o está incompleta (formato HH:MM)
if (/^\d{2}:\d{2}/.test(timePart) === false) {
const now = new Date();
const hh = String(now.getHours()).padStart(2, '0');
const mm = String(now.getMinutes()).padStart(2, '0');
timePart = hh + ':' + mm;
}

// normaliza el valor al formato esperado por datetime-local: YYYY-MM-DDTHH:MM
input.value = datePart + 'T' + timePart.substring(0, 5);
}
10 changes: 9 additions & 1 deletion Core/Lib/Widget/WidgetDatetime.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

namespace FacturaScripts\Core\Lib\Widget;

use FacturaScripts\Core\Lib\AssetManager;
use FacturaScripts\Core\Request;
use FacturaScripts\Core\Tools;

/**
* Description of WidgetDatetime
Expand All @@ -28,9 +30,15 @@
*/
class WidgetDatetime extends BaseWidget
{
protected function assets(): void
{
$route = Tools::config('route');
AssetManager::addJs($route . '/Dinamic/Assets/JS/WidgetDatetime.js?v=1');
}

protected function inputHtml($type = 'datetime-local', $extraClass = '')
{
$class = $this->combineClasses($this->css('form-control'), $this->class, $extraClass);
$class = $this->combineClasses($this->css('form-control'), 'widget-datetime', $this->class, $extraClass);
$value = empty($this->value) ? '' : date('Y-m-d H:i:s', strtotime($this->value));
return '<input type="' . $type . '" name="' . $this->fieldname . '" value="' . $value
. '" class="' . $class . '"' . $this->inputHtmlExtraParams() . '/>';
Expand Down