Los mensajes de error de un componente, se muestran en otro conocido como
texto de ayuda, que normalmente usa un elemento
small
con el atributo
slot="supporting".
La forma usa el atributo
novalidate.
En este ejemplo, el campo
select-menu
con el id
selectGenero
usa el evento
input.
para invocar la función
copiaMensajes.
y copiar los mensajes de error del campo de captura al texto de ayuda.
| 1 | <!DOCTYPE html> |
| 2 | <html lang="es" class="light dark"> |
| 3 | |
| 4 | <head> |
| 5 | |
| 6 | <meta charset="UTF-8"> |
| 7 | <title>Formulario - PWA con MD</title> |
| 8 | |
| 9 | <script src="js/registraServiceWorker.js"></script> |
| 10 | |
| 11 | <meta name="viewport" content="width=device-width"> |
| 12 | <meta name="theme-color" content="#fffbfe"> |
| 13 | <link rel="icon" sizes="32x32" href="favicon.ico"> |
| 14 | <link rel="manifest" href="site.webmanifest"> |
| 15 | <script src="ungap/custom-elements.js"></script> |
| 16 | |
| 17 | <script type="module" src="js/configura.js"></script> |
| 18 | <link rel="stylesheet" href="css/estilos.css"> |
| 19 | <link rel="stylesheet" href="css/transicion_completa.css"> |
| 20 | |
| 21 | </head> |
| 22 | |
| 23 | <body> |
| 24 | |
| 25 | <form id="form" novalidate onsubmit="procesa(event)"> |
| 26 | |
| 27 | <md-top-app-bar> |
| 28 | |
| 29 | <h1>Formulario</h1> |
| 30 | |
| 31 | <button is="md-menu-button" slot="navigation"></button> |
| 32 | |
| 33 | </md-top-app-bar> |
| 34 | |
| 35 | <main> |
| 36 | |
| 37 | <p> |
| 38 | <span class="md-filled-text-field" accesskey="G"> |
| 39 | <md-select-menu id="selectGenero" required options="opcionesDeGenero" |
| 40 | oninput="copiaMensajes()"></md-select-menu> |
| 41 | <span>Género *</span> |
| 42 | <small id="supportingGenero">Obligatorio</small> |
| 43 | </span> |
| 44 | </p> |
| 45 | |
| 46 | <p> |
| 47 | <button class="md-filled-button" style="width: 100%;">Recomendar</button> |
| 48 | </p> |
| 49 | |
| 50 | </main> |
| 51 | |
| 52 | <md-options-menu id="opcionesDeGenero" aria-label="Opciones de género"> |
| 53 | <span data-value="" title="Selecciona género"></span> |
| 54 | <span data-value="pop">Pop</span> |
| 55 | <span data-value="reg">Reguetón</span> |
| 56 | </md-options-menu> |
| 57 | |
| 58 | <nav-drw></nav-drw> |
| 59 | |
| 60 | </form> |
| 61 | |
| 62 | <script type="module"> |
| 63 | import { muestraTextoDeAyuda } from "./lib/js/muestraTextoDeAyuda.js" |
| 64 | import { exportaAHtml } from "./lib/js/exportaAHtml.js" |
| 65 | import { muestraError } from "./lib/js/muestraError.js" |
| 66 | |
| 67 | function copiaMensajes() { |
| 68 | muestraTextoDeAyuda(selectGenero, supportingGenero, "Obligatorio") |
| 69 | } |
| 70 | exportaAHtml(copiaMensajes) |
| 71 | |
| 72 | /** |
| 73 | * @param {SubmitEvent} evt |
| 74 | */ |
| 75 | function procesa(evt) { |
| 76 | evt.preventDefault() |
| 77 | try { |
| 78 | copiaMensajes() |
| 79 | if ( |
| 80 | selectGenero.validity.valid) { |
| 81 | const genero = selectGenero.value |
| 82 | const resultado = recomienda(genero) |
| 83 | alert(resultado) |
| 84 | } |
| 85 | } catch (e) { |
| 86 | muestraError(e) |
| 87 | } |
| 88 | } |
| 89 | exportaAHtml(procesa) |
| 90 | |
| 91 | /** @param {string} genero */ |
| 92 | function recomienda(genero) { |
| 93 | if (genero === "pop") { |
| 94 | return "Para el pop te recomiendo a Dua Lipa." |
| 95 | } else if (genero === "reg") { |
| 96 | return "Para el reguetón te recomiendo a Bad Bunny." |
| 97 | } |
| 98 | } |
| 99 | </script> |
| 100 | |
| 101 | </body> |
| 102 | |
| 103 | </html> |