npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sadrix-vue-form

v1.2.0

Published

Simple Javascript classes for handle form fields and errors.

Downloads

21

Readme

Sadrix Vue Form

Simple Javascript classes for handle form fields and errors.

Installation

Install latest version with npm:

npm i sadrix-vue-form

View package on npm website.

How to use

Add plugin to Vue app

Simply import package from npm and add it as a plugin to your Vue app:

import { createApp } from 'vue';
import VueForm from 'sadrix-vue-form';

const app = createApp();
app.use(VueForm, 'Form');
app.mount('#app');

Define new Form data property

As you can see in .use(VueForm, 'Form'), second property is the name of global prop for Form class in your application. So feel free to set any name you want. The default will be 'Form' with capital 'F'.

After adding plugin to your application, now you can use it in two ways:

<script>
export default {
    data() {
        return {
            form: new Form({
                username: '',
                password: ''
            })
        }
    }
}
</script>

or

<script>
export default {
    data() {
        return {
            form: new this.$Form({
                username: '',
                password: ''
            })
        }
    }
}
</script>

<script Setup> format

<script setup>
import { ref } from 'vue'

const form = ref({
    username: '',
    password: ''
})
</script>

Access form field value in Vue template

If you add Form as component data property, Now you can set input value to form field like this:

<template>
    <div>
        <label for="username">Username:</label>
        <input type="text" v-model="form.username" id="username">
    </div>
    <div>
        <label for="password">Password:</label>
        <input type="password" v-model="form.password"  id="password">
    </div>
</template>

Check form passed or has errors

Checking your form whether has errors or not will be useful sometimes. Just use this method and it will return a boolean.

this.form.errors.passed()

Error handling

1. Set errors

If you want to set errors for your form fields:

// set() method will reset form errors and set it to new errors
this.form.errors.set({
    username: 'This username taken before',
    password: 'Password must contain at least one character'
});

// set() method with second param, don't reset form and set exact fields errors passed in second parameter
this.form.errors.set({
    username: 'This username taken before',
    password: 'Password must contain at least one character'
}, [
    // this array say only this fields should set and dont reset all errors
    'username'
]);

// add() method will add or rewrite field error (Not refresh form errors)
this.form.errors.add({
    password: 'Password must contain at least one character'
});

2. Get field error

If field with certain name has any error, this method will return it otherwise will return empty string:

// get() method will return form error or empty string
this.form.errors.get('username');

3. Clear form errors

By using .clear() with no params, all form errors will be removed. If you pass a field name, only field error will be removed. You can pass an array of field names too.

// clear() method
// clear all errors
this.form.errors.clear();

// clear single field error
this.form.errors.clear('username');

// clear multi fields errors
this.form.errors.clear(['username', 'password']);