Crafting robust forms with clear and maintainable validation is vital for any SPA application. Existing libraries might leave you with cluttered templates, complex rule configurations, and scattered error handling. it does have some potential drawbacks in terms of code readability, maintainability, and usability:
| Aspect | Drawbacks |
|---|---|
| Code Readability | Inline validation rules can clutter templates |
| Maintainability | Complex rule configuration and spread-out error handling. |
| Usability | Customization overhead and potential for errors in templates. |
But there's a better way! With @tidy validation package tackles these challenges head-on, offering a streamlined and developer-friendly solution built with comprehensive type safety.
This example demonstrates how @tidy simplifies form validation in Vue.js. We'll create a basic form with a single name input field and ensure it's required using @tidy's declarative validation approach..
<script setup lang="ts">
import { reactive } from 'vue'
import { useForm, Validators } from "@nattyjs/tidy"
const form = reactive(
useForm({ name: '' }, {
validators: {
name: Validators.required()
}
})
)
</script>
<template>
<div>
<input v-model="form.name" />
<span v-if="form.errors?.name">This field is required</span>
</div>
</template>
You can start playing with this template in your browser using our online sandboxes: