WlField

Label, hint and error for a form field. The slot provides id, ariaDescribedby and invalid — connect them to the field.
Components / Miscellaneous v0.1.0

Change props to see the result: the example and code use the same settings. “Reset” restores the initial values.

Settings

label

Field label.

hint

Hint below the field.

error

Error text. The hint stays visible.

Asterisk for a required field.

Announce changing validation errors; static errors are described by the control.

Show the localized optional marker when required is false.

Props id, pt, orientation, models, handlers and slots can be edited in the SFC. See their descriptions in API.

Example

Up to 80 characters.
Optional. The visible label names the select through labelId; Clear removes the selection.
Focus: outside the example · pointer: outside the example

Explore the live example and copy its Vue SFC into your application. Settings on the left can be combined.

Code for your application

Vue SFC · current settings
<script setup lang="ts">
import { WlButton, WlCheckbox, WlField, WlInput, WlSelect } from "legato-ui";
import { computed, ref } from "vue";
const value = ref("");
const status = ref<string | null>(null);
const error = ref("");
const fluid = ref(false);
const options = computed(() => [
  { label: "Draft", value: "draft" },
  { label: "Published", value: "published" }
]);
function validate(): void {
  error.value = value.value.trim() ? "" : "Enter a material title, then validate again.";
}
</script>

<template>
  <form
    class="wl-stack"
    :class="{ 'wl-fluid': fluid }"
    data-space="lg"
    novalidate
    @submit.prevent="validate"
  >
    <WlCheckbox v-model="fluid">{{ "Fill the form width (wl-fluid)" }}</WlCheckbox>
    <WlField
      v-slot="field"
      label="Material title"
      required
      hint="Up to 80 characters."
      :error="error"
      announce-error
      
    ><WlInput
      :id="field.id"
      v-model="value"
      :invalid="field.invalid"
      :required="field.required"
      :aria-labelledby="field.labelId"
      :aria-describedby="field.ariaDescribedby"
      maxlength="80"
    /></WlField>
    <WlField
      v-slot="field"
      label="Status"
      hint="Optional. The visible label names the select through labelId; Clear removes the selection."
      optional
      orientation="horizontal"
    >
      <WlSelect
        :id="field.id"
        v-model="status"
        :options="options"
        option-label="label"
        option-value="value"
        clearable
        :aria-labelledby="field.labelId"
        :aria-describedby="field.ariaDescribedby"
      />
    </WlField>
    <div class="wl-inline" data-space="sm">
      <WlButton type="submit" size="sm">{{ "Validate fields" }}</WlButton>
      <WlButton size="sm" variant="ghost" @click="value = ''; status = null; error = ''">{{ "Start over" }}</WlButton>
    </div>
  </form>
</template>

Field label, hint and error

Required and optional fields, hint plus error, horizontal layout, labelId for Select, email/readonly/textarea and local validation.

The address is used only in the local example. Enter an email for notifications.
The field is readable but cannot be changed.
The label and hint are associated with the textarea through scope.
Optional. The visible label names the select through labelId; Clear removes the selection.

Check the message below Email.

Vue SFC · additional example
<script setup lang="ts">
import { WlButton, WlField, WlInput, WlSelect, WlTextarea } from "legato-ui";
import { computed, ref } from "vue";
const email = ref("");
const note = ref("");
const error = ref("Enter an email for notifications.");
const saved = ref(false);
const status = ref<string | null>(null);
const options = computed(() => [
  { label: "Draft", value: "draft" },
  { label: "Published", value: "published" }
]);
function validate(): void {
  saved.value = /^[^\s@]+@[^\s@][^\s.@]*\.[^\s@]+$/.test(email.value);
  error.value = saved.value ? "" : "This example requires an email such as name@example.org.";
}
</script>

<template>
  <form
    class="wl-stack"
    data-space="lg"
    novalidate
    @submit.prevent="validate"
  >
    <WlField
      label="Notification email"
      required
      hint="The address is used only in the local example."
      :error="error"
    ><template #default="{ id, labelId, ariaDescribedby, ariaInvalid, invalid, required }"><WlInput
      :id="id"
      v-model="email"
      type="email"
      :required="required"
      :invalid="invalid"
      :aria-invalid="ariaInvalid"
      :aria-labelledby="labelId"
      :aria-describedby="ariaDescribedby"
    /></template></WlField>
    <WlField label="Material ID" hint="The field is readable but cannot be changed.">
      <template #default="{ id, ariaDescribedby }">
        <WlInput
          :id="id"
          model-value="GAV-24"
          readonly
          :aria-describedby="ariaDescribedby"
        />
      </template>
    </WlField>
    <WlField
      label="Comment"
      hint="The label and hint are associated with the textarea through scope."
    >
      <template #default="{ id, ariaDescribedby }">
        <WlTextarea :id="id" v-model="note" :aria-describedby="ariaDescribedby" />
      </template>
    </WlField>
    <WlField
      v-slot="field"
      label="Status"
      hint="Optional. The visible label names the select through labelId; Clear removes the selection."
      optional
      orientation="horizontal"
    >
      <WlSelect
        :id="field.id"
        v-model="status"
        :options="options"
        option-label="label"
        option-value="value"
        clearable
        :aria-labelledby="field.labelId"
        :aria-describedby="field.ariaDescribedby"
      />
    </WlField>
    <div class="wl-inline" data-space="sm">
      <WlButton type="submit" size="sm" variant="primary">{{ "Validate fields" }}</WlButton>
      <WlButton
        size="sm"
        variant="ghost"
        @click="email = ''; error = 'Enter an email for notifications.'; saved = false"
      >{{ "Restore field error" }}</WlButton>
    </div>
    <p class="wl-text-small" role="status">
      {{ saved ? "Fields passed local validation." : "Check the message below Email." }}
    </p>
  </form>
</template>