All Projects → spatie → Form Backend Validation

spatie / Form Backend Validation

Licence: mit
An easy way to validate forms using back end logic

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Form Backend Validation

react-native-validator-form
Simple validator for react-native forms.
Stars: ✭ 21 (-97.32%)
Mutual labels:  validation, form
svelte-form
JSON Schema form for Svelte v3
Stars: ✭ 47 (-94.01%)
Mutual labels:  validation, form
datalize
Parameter, query, form data validation and filtering for NodeJS.
Stars: ✭ 55 (-92.98%)
Mutual labels:  validation, form
Pristine
Vanilla javascript form validation micro-library
Stars: ✭ 197 (-74.87%)
Mutual labels:  validation, form
Bunny
BunnyJS - Lightweight native (vanilla) JavaScript (JS) and ECMAScript 6 (ES6) browser library, package of small stand-alone components without dependencies: FormData, upload, image preview, HTML5 validation, Autocomplete, Dropdown, Calendar, Datepicker, Ajax, Datatable, Pagination, URL, Template engine, Element positioning, smooth scrolling, routing, inversion of control and more. Simple syntax and architecture. Next generation jQuery and front-end framework. Documentation and examples available.
Stars: ✭ 473 (-39.67%)
Mutual labels:  validation, form
Resolvers
📋 Validation resolvers: Zod, Yup, Joi, Superstruct, and Vest.
Stars: ✭ 222 (-71.68%)
Mutual labels:  validation, form
Fore
Fore - declarative programming with web components
Stars: ✭ 34 (-95.66%)
Mutual labels:  validation, form
Simple React Validator
A simple react form validator inspired by Laravel validation.
Stars: ✭ 170 (-78.32%)
Mutual labels:  validation, form
React Hook Form
📋 React Hooks for form state management and validation (Web + React Native)
Stars: ✭ 24,831 (+3067.22%)
Mutual labels:  validation, form
Approvejs
A simple JavaScript validation library that doesn't interfere
Stars: ✭ 336 (-57.14%)
Mutual labels:  validation, form
Redux Form
A Higher Order Component using react-redux to keep form state in a Redux store
Stars: ✭ 12,597 (+1506.76%)
Mutual labels:  validation, form
Vvalidator
🤖 An easy to use form validator for Kotlin & Android.
Stars: ✭ 592 (-24.49%)
Mutual labels:  validation, form
React Advanced Form
Functional reactive forms. Multi-layer validation, custom styling, field grouping, reactive props, and much more.
Stars: ✭ 186 (-76.28%)
Mutual labels:  validation, form
node-input-validator
Validation library for node.js
Stars: ✭ 74 (-90.56%)
Mutual labels:  validation, form
Qmbform
Create simple Android forms
Stars: ✭ 184 (-76.53%)
Mutual labels:  validation, form
formalizer
React hooks based form validation made for humans.
Stars: ✭ 12 (-98.47%)
Mutual labels:  validation, form
Formhelper
ASP.NET Core - Transform server-side validations to client-side without writing any javascript code. (Compatible with Fluent Validation)
Stars: ✭ 155 (-80.23%)
Mutual labels:  validation, form
Neoform
✅ React form state management and validation
Stars: ✭ 162 (-79.34%)
Mutual labels:  validation, form
Validate
A simple jQuery plugin to validate forms.
Stars: ✭ 298 (-61.99%)
Mutual labels:  validation, form
Validator.js
⁉️轻量级的 JavaScript 表单验证,字符串验证。没有依赖,支持 UMD ,~3kb。
Stars: ✭ 486 (-38.01%)
Mutual labels:  validation, form

An easy way to validate forms using back end logic

Latest Version on NPM Software License Build Status npm

Wouldn't it be great if you could just use your back end to validate forms on the front end? This package provides a Form class that does exactly that. It can post itself to a configured endpoint and manage errors. The class is meant to be used with a Laravel back end.

Take a look at the usage section to view a detailed example on how to use it.

The code of this package is based on the Object-Oriented Forms lesson in the Vue 2.0 series on Laracasts.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Install

You can install the package via yarn (or npm):

yarn add form-backend-validation

By default, this package expects axios to be installed (unless you're using your own http library, see the Options section for that).

yarn add axios

Usage

You can find an example implementation with Laravel and Vue in the spatie/form-backend-validation-example-app repo.

Screenshot

import Form from 'form-backend-validation';

// Instantiate a form class with some values
const form = new Form({
    field1: 'value 1',
    field2: 'value 2',
    person: {
        first_name: 'John',
        last_name: 'Doe',
    },
});

// A form can also be initiated with an array
const form = new Form(['field1', 'field2']);

// Submit the form, you can also use `.put`, `.patch` and `.delete`
form.post(anUrl)
   .then(response => ...)
   .catch(response => ...);

// Returns true if request is being executed
form.processing;

// If there were any validation errors, you easily access them

// Example error response (json)
{
    "errors": {
        "field1": ['Value is required'],
        "field2": ['Value is required']
    }
}

// Returns an object in which the keys are the field names
// and the values array with error message sent by the server
form.errors.all();

// Returns true if there were any error
form.errors.any();

// Returns object with errors for the specified keys in array.
form.errors.any(keys);

// Returns true if there is an error for the given field name or object
form.errors.has(key);

// Returns the first error for the given field name
form.errors.first(key);

// Returns an array with errors for the given field name
form.errors.get(key);

// Shortcut for getting the first error for the given field name
form.getError(key);

// Clear all errors
form.errors.clear();

// Clear the error of the given field name or all errors on the given object
form.errors.clear(key);

// Returns an object containing fields based on the given array of field names
form.only(keys);

// Reset the values of the form to those passed to the constructor
form.reset();

// Set the values which should be used when calling reset()
form.setInitialValues();

// Populate a form after its instantiation, the populated fields will override the initial fields
// Fields not present at instantiation will not be populated
const form = new Form({
    field1: '',
    field2: '',
});

form.populate({
    field1: 'foo',
    field2: 'bar',
});

Options

The Form class accepts a second options parameter.

const form = new Form({
    field1: 'value 1',
    field2: 'value 2',
}, {
    resetOnSuccess: false,
});

You can also pass options via a withOptions method (this example uses the create factory method.

const form = Form.create()
    .withOptions({ resetOnSuccess: false })
    .withData({
        field1: 'value 1',
        field2: 'value 2',
    });

resetOnSuccess: bool

Default: true. Set to false if you don't want the form to reset to its original values after a succesful submit.

http: Object

By default this library uses axios for http request. If you want, you can roll with your own http library (or your own axios instance).

Advanced! Pass a custom http library object. Your http library needs to adhere to the following interface for any http method you're using:

method(url: string, data: Object): Promise<Response>

Supported http methods are get, delete, head, post, put & patch.

If you want to see how the http library is used internally, refer to the Form class' submit method.

Working with files

The form handles file inputs too. The data is then sent as FormData, which means it's encoded as multipart/form-data.

Some frameworks (like Laravel, Symfony) can't handle these incoming requests through other methods than POST, so you might need to take measures to work around this limitation. In Laravel or Symfony, that would mean adding a hidden _method field to your form containing the desired HTTP verb.

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ npm run test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please contact Freek Van der Herten instead of using the issue tracker.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

Initial code of this package was copied from Jeffrey Way's Vue-Forms repo.

The idea to go about this way of validating forms comes from Laravel Spark.

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].