All Projects β†’ sukima β†’ ember-validity-modifier

sukima / ember-validity-modifier

Licence: MIT license
Ember Octane addon to add custom validity (form validation) to form fields

Programming Languages

javascript
184084 projects - #8 most used programming language
Handlebars
879 projects
CSS
56736 projects
HTML
75241 projects

Projects that are alternatives of or similar to ember-validity-modifier

Jafar
🌟!(Just another form application renderer)
Stars: ✭ 107 (+282.14%)
Mutual labels:  forms, form, form-validation
Redux Form
A Higher Order Component using react-redux to keep form state in a Redux store
Stars: ✭ 12,597 (+44889.29%)
Mutual labels:  forms, form, form-validation
Form For
ReactJS forms made easy
Stars: ✭ 118 (+321.43%)
Mutual labels:  forms, form, form-validation
React Hook Form
πŸ“‹ React Hooks for form state management and validation (Web + React Native)
Stars: ✭ 24,831 (+88582.14%)
Mutual labels:  forms, form, form-validation
FrontendForms
A module for ProcessWire CMS to create and validate forms on the frontend easily using the Valitron library.
Stars: ✭ 0 (-100%)
Mutual labels:  forms, form, form-validation
React Final Form
🏁 High performance subscription-based form state management for React
Stars: ✭ 6,781 (+24117.86%)
Mutual labels:  forms, form, form-validation
Composable Form
Build type-safe composable forms in Elm
Stars: ✭ 179 (+539.29%)
Mutual labels:  forms, form, form-validation
form-data-json
A zero dependency, cross browser library to easily get or set/manipulate form input values as/from a json object.
Stars: ✭ 37 (+32.14%)
Mutual labels:  forms, form, form-validation
ember-foxy-forms
Ember Addon for Making Foxy Forms
Stars: ✭ 27 (-3.57%)
Mutual labels:  ember, forms, ember-addon
Final Form
🏁 Framework agnostic, high performance, subscription-based form state management
Stars: ✭ 2,787 (+9853.57%)
Mutual labels:  forms, form, form-validation
grav-plugin-form
Grav Form Plugin
Stars: ✭ 48 (+71.43%)
Mutual labels:  forms, form, form-validation
ember-rapid-forms
Smart, Intuitive forms for Ember.js styled with Bootstrap, Multi layouts and Validation support.
Stars: ✭ 58 (+107.14%)
Mutual labels:  ember, ember-addon, form
vue-use-form
βœ… A Vue.js composition API function to validate forms
Stars: ✭ 97 (+246.43%)
Mutual labels:  forms, form, form-validation
Usetheform
React library for composing declarative forms, manage their state, handling their validation and much more.
Stars: ✭ 40 (+42.86%)
Mutual labels:  forms, form, form-validation
react-cool-form
😎 πŸ“‹ React hooks for forms state and validation, less code more performant.
Stars: ✭ 246 (+778.57%)
Mutual labels:  forms, form, form-validation
React Controlled Form
Flexible, Modular & Controlled Forms for React and Redux
Stars: ✭ 121 (+332.14%)
Mutual labels:  forms, form, form-validation
Form bloc
πŸ”₯ Dart and Flutter Package πŸ”₯ Easy Form State Management using BLoC pattern πŸ”₯ Wizard/stepper forms, asynchronous validation, dynamic and conditional fields, submission progress, serialization and more! πŸ”₯
Stars: ✭ 239 (+753.57%)
Mutual labels:  forms, form, form-validation
ember-do-forms
ember-do-forms handles the icky parts of forms that you don't want to, and leaves the rest to you.
Stars: ✭ 18 (-35.71%)
Mutual labels:  ember, forms, ember-addon
Ember Form For
Stars: ✭ 136 (+385.71%)
Mutual labels:  ember, forms, ember-addon
react-apollo-form
Build React forms based on GraphQL APIs.
Stars: ✭ 195 (+596.43%)
Mutual labels:  forms, form

ember-validity-modifier

A very simple validation addon using a custom modifier. This makes adding custom validations to form elements as simple as adding a modifier to the field along with your own helper or validation function.

Demo

Compatibility

  • Ember.js v3.16 or above
  • Ember CLI v2.13 or above
  • Node.js v10 or above

Installation

ember install ember-validity-modifier

Usage

Example using a component action

<input {{validity this.validate}}>
export default MyComponent extends Component {
  @action
  validate({ value }) {
    return value === 'foobar' ? [] : ['Must be a foobar'];
  }
}

Example using a custom helper

<input {{validity (validate-foobar)}}>
export default helper(function validateFoobar() {
  return ({ value }) => value === 'foobar' ? [] : ['Must be a foobar'];
});

Example using native validations

<input required pattern="foobar" {{validity}}>

Example using more than one validations

<input {{validity (validate-present) (validate-phone-number)}}>

Example of only validating on specific events

By default validation will happen on change, input, and blur. Comma separate event names.

<input {{validity (validate-foobar) on="change,input"}}>

Example adding to non form fields (bubbling)

In cases where we don't have easy access to the form field itself we can also add it to a parent element. This might be the case when adding a modifier that gets applied by another component via its ...attributes.

<div {{validity (validate-foobar)}}>
  <label for="example">Example</label>
  <input id="example">
</div>

Example validation on form submit

<form
  ...attributes
  {{verify-form-validity submit=this.handleSubmit reportValidity=true}}
>
  <label for="firstName">First name</label>
  <input type="text" name="firstName" id="firstName" required {{validity}}>
  <label for="lastName">Last name</label>
  <input
    type="text"
    name="lastName"
    id="lastName"
    required
    {{validity (validate-not-match "firstName")}}
  >
  <button type="submit">Submit form</button>
</form>
import Component from '@glimmer/component';
import { validate } from 'ember-validity-modifier';

export default class MyForm extends Component {
  @action
  handleSubmit({ target: form }) {
    console.log('Fake submit action', Object.fromEntries(new FormData(form)));
  }
}

Example with validateImmediately argument

To validate the form state on initial render add validateImmediately=true.

  <input {{validity
    (fn this.matchTo this.match)
    on="change"
    validateImmediately=true
  }}>

Example with validateTracked argument

To validate the form state when initial render and any time one of its dependent arguments change, add the 'validateTracked' argument with the dependent properties.

Because the validator argument is a function it is possible to not exercise the tracked properties and thus miss out on validations when those tracked properties change. This is the case of the fn helper which lazy executes thus doesn't trigger Ember's auto-tracking if it isn't ran first.

To compensate we can use validateTracked to inform the modifier that it needs to run the validations when these properties change.

  <input {{validity
    (fn this.matchTo this.match)
    on="change"
    validateTracked=this.match
  }}>

To validate the form state any time any of its dependent arguments change, add the validateTracked argument using the array helper and a list of dependent properties.

  <input {{validity
    (fn this.matchTo this.match1 this.match2)
    on="change"
    validateTracked=(array this.match1 this.macth2)
  }}>

Example with select

<select name="foobar" {{validity (validate-selected-option)}}>
  <option value="">β€”Pick oneβ€”</option>
  <option value="foo">Foo</option>
  <option value="bar">Bar</option>
  <option value="baz">Baz</option>
</select>
export default helper(function validateSelectedOption() {
  return ({ name, value }) => value === '' ? [`Must pick an option for ${name}`] : [];
});

Example with ember-changeset validations

{{#let (changeset this.data this.validate) as |subject|}}
  <Input
    @value={{subject.foobar}}
    {{validity (validate-changeset subject "foobar")}}
  />
{{/let}}
export default helper(function validateChangeset([changeset, prop]) {
  return async () => {
    await changeset.validate(prop);
    let { validation: error } = changeset.error[prop] ?? {};
    return error ? [error] : [];
  };
});

Example rendering validation messages

{{#let (form-errors) as |errors|}}
  <input
    name="foobar"
    {{on "validated" errors.update}}
    {{validity (validate-foobar)}}
  >
  <span>{{errors.message.foobar}}</span>
{{/let}}

form-error exposes the following:

  • update β€” action to process a validated event
  • set β€” action that can set specific fields
  • for.<name> β€” the errors as an array
  • native.<name> β€” any native errors as an array
  • custom.<name> β€” any custom errors as an array
  • message.<name> β€” the validationMessage from the DOM element

Example CSS

/* All the things */
:valid { … }
:invalid { … }

/* Not on first render, use the validated event to set dataset.validated */
[data-validited]:valid { … }
[data-validited]:invalid { … }

How this works

The blog post Managing validity in forms takes a dive into a simple native (vanilla) implementation of this idea. In the post it describes the idea that validations can be managed through DOM events. By attaching the validation functions to an event handler they can easily manage the native custom validity of the element.

When a validate event is dispatched (by default the events are validate, input, change, and blur). Each validator function registered will be evaluated, the results will be consolidated, and the element's custom validity is set, finally a validated event is dispatched to announce that the process is complete (in case of asynchronous validations).

Sequence diagram of the validation events

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.

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].