This package provides a Form class that can validation our forms with laravel backend validation logic. The class is meant to be used with a Laravel back end.
Install
You can install the package via yarn (or npm):
$ npm install @imritesh/form
$ yarn add @imritesh/form
By default, this package expects axios to be installed
importFormfrom'form-backend-validation';// Instantiate a form class with some valuesconstform=newForm({field1: 'value 1',field2: 'value 2',person: {first_name: 'John',last_name: 'Doe',},});// A form can also be initiated with an arrayconstform=newForm(['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 executedform.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 serverform.errors.all();// Returns true if there were any errorform.errors.any();// Returns true if there is an error for the given field name or objectform.errors.has(key);// Returns the first error for the given field nameform.errors.first(key);// Returns an array with errors for the given field nameform.errors.get(key);// Shortcut for getting the errors for the given field nameform.getError(key);// Clear all errorsform.errors.clear();// Clear the error of the given field name or all errors on the given objectform.errors.clear(key);// Returns an object containing fields based on the given array of field namesform.only(keys);// Reset the values of the form to those passed to the constructorform.reset();// Set the values which should be used when calling reset()form.setInitialValues();// Populate a form after its instantiation, the populated fields won't override the initial fields// Fields not present at instantiation will not be populatedconstform=newForm({field1: '',field2: '',});form.populate({field1: 'foo',field2: 'bar',});
Options
The Form class accepts a second options parameter.
vue-coe-validator ✅ Another validation form for the Vue. Validates input fields of multiple forms and displays errors. Note: without any dependencies.