Nuxt - Laravel Precognition
GitHubNuxt Module
  • Getting Started
    • Introduction
    • Installation
  • Usage
    • Configuration
  • Validation
  • Error handling
  • File uploads
  • Use with Nuxt UI
  • Composables
    • usePrecognitionForm
    • usePrecognitionConfig
    • useNuxtFormValidator
  • Other
    • Breeze Nuxt template
    • Troubleshooting
Powered by GitBook
On this page

Use with Nuxt UI

How to integrate Pracognition into Nuxt UI forms.

PreviousFile uploadsNextusePrecognitionForm

Last updated 1 month ago

Since uses its own validation process, there is no way to integrate it with the Precognition protocol directly. However, we can define by passing a function as a form option.

This module provides a special composable - , which returns a validator function compatible with Nuxt UI Form.

Here is an example of using Nuxt UI Form along with Precognition validation:

MyForm.vue
<script setup lang="ts">
type LoginForm = {
  email: string
  password: string
  remember_me?: boolean
}

const payload: LoginForm = {
  email: '',
  password: '',
  remember_me: false,
}

const form = usePrecognitionForm<LoginForm>('post', '/login', payload)
const validator = useNuxtFormValidator(form)
const state: LoginForm = form.fields

async function onSubmit(_: FormSubmitEvent<LoginForm>) {
  try {
    const response = await form.submit()
  }
  catch {
    console.error('Form submission error')
  }
}
</script>

<template>
  <UForm
    :validate="validator"
    :state="state"
    @submit="onSubmit"
  >
    <UFormField
      label="Email"
      name="email"
    >
      <UInput
        v-model="state.email"
        placeholder="Email"
      />
    </UFormField>

    <UFormField
      label="Password"
      name="password"
    >
      <UInput
        v-model="state.password"
        placeholder="Password"
        type="password"
      />
    </UFormField>

    <UButton type="submit">
      Submit
    </UButton>
  </UForm>
</template>

While using Nuxt UI Form, validation of the fields is triggered automatically according to the form's settings.

Keep in mind that validation and field data manipulation should be done using the Nuxt UI Form instead of the Precognition form in this case. Check how to access Nuxt UI Form methods here - .

Nuxt UI Form
custom validation
useNuxtFormValidator
Form Expose