add validation

This commit is contained in:
Pedro Pérez 2025-12-09 19:16:47 +01:00
parent cfa0734f08
commit aa8410c723

View File

@ -1,6 +1,6 @@
<li <li
id="todo-{{.ID}}" id="todo-{{.ID}}"
x-data="{ editing: false, name: '{{.Name}}', backup: '{{.Name}}' }" x-data="{ editing: false, name: '{{.Name}}', backup: '{{.Name}}', error: '' }"
@htmx:response-error="name = backup; $dispatch('show-toast', 'Error al actualizar. Inténtelo de nuevo.');" @htmx:response-error="name = backup; $dispatch('show-toast', 'Error al actualizar. Inténtelo de nuevo.');"
> >
<div x-show="!editing" @dblclick="editing = true" x-text="name"> <div x-show="!editing" @dblclick="editing = true" x-text="name">
@ -12,17 +12,27 @@
hx-put="/todo/{{.ID}}/update" hx-put="/todo/{{.ID}}/update"
hx-target="closest li" hx-target="closest li"
hx-swap="outerHTML" hx-swap="outerHTML"
@submit="editing = false" @htmx:before-request="
if (name.trim().length <3) {
$event.preventDefault();
error = 'Debe tener 3 caracteres como mínimo';
return;
} else {
error = '';
editing = false;}"
> >
<input <input
type="text" type="text"
name="name" name="name"
value="{{ .Name }}" value="{{ .Name }}"
@keydown.escape="name = backup; editing = false" @keydown.escape="name = backup; editing = false"
@input="error = ''"
x-model="name" x-model="name"
x-init="$el.focus()" x-init="$el.focus()"
/> />
<span x-show="error" x-text="error"></span>
<button type="button" @click="name =backup; editing = false"> <button type="button" @click="name =backup; editing = false">
Cancelar Cancelar
</button> </button>