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

ember-stickler

v0.1.0-beta.2

Published

Flexible Component based DDAU form validation handling for Ember

Downloads

2

Readme

NPM Version NPM Downloads NPM License Travis Build Status Code Climate Code Climate Coverage

Ember Stickler

Flexible DDAU form validation handling

Ember Stickler will handle errors from your server as easily and gracefully as errors from the client. If you've ever skimped on form validation or form UX, and felt guilty, this library is your new guilty pleasure.

Stickler is comprised of two components: validated-form and and the contextual component form.validation, which manage your data and error routing, and provide an easy access point to utilize validations.

Installation

ember install ember-stickler

Usage:


{{#validated-form action=(action submitForm model) as |form|}}

  {{!-- with native inputs --}}
  {{#form.validation rules="exists" as |validation|}}

    <label for="intensity">Department To Route Campaign Responses To</label>
    <input type="range" id="intensity" value={{someValue}} min=0 max=11 oninput={{action validation.validate value="target.value"}}>

    {{#if validation.errors}}
      <div class="text-danger">{{first validation.errors}}</div>
    {{/if}}

 {{/form.validation}}
 
 {{!-- with Ember inputs --}}
 {{#form.validation minLength=8 rules="trim required min-length" as |v|}}
   {{input
       class=(join ' ' 'factsumo-input' (for-bool v.state.valid 'has-success' 'has-error'))
       type="password"
       autocomplete="new-password"
       placeholder='Password'
       value=myNewPassword
       focus-out=(action v.validate myNewPassword)
       input=(action v.check myNewPassword)
   }}
 {{/form.validation}}


 {{!-- with a native select --}}
 {{#form.validation rules="exists" as |validation|}}

     <select onchange={{action validation.validate value="target.value"}}>
       {{#each choices as |choice|}}
           <option value={{choice}} selected={{is-equal currentValue choice}}>{{choice}}</option>
       {{/each}}
     </select>

 {{/form.validation}}

  {{!-- submit buttons --}}
  <button type="submit" disabled={{form.state.disabled}} {{action form.submit}}>Submit</button>

{{/validated-form}}

Validation Rules

Validations are added to form.validation components by specifying the rules attribute. Separate multiple rules by a space, rules will be run left to right, and special transform rules (such as trim can manipulate the value for cleaner comparisons.

  {{#form.validation
      submitErrors=form.errors.firstName
      rules='required min-length'
      minLength=5
      minLengthMessage='a minimum of 5 characters is required'
      as |validation|
    }}
      ...
  {{/form.validation}}

Validated Form Component

validated-form submits by calling sendAction on whatever action attr you add and will default to submit. It invokes the action passing the following arguments reset, resolve, reject.

The validated-form yields a hash with the following properties:

  1. submit this will trigger the validations in each of the validation-wrappers and will submit if they all pass.

  2. reset sets all the validation state and errors back to their initial state and changes the form state from pending to resolved and reject takes an error object with keys being the name of the field and value an array of error message strings.

  3. state an object with the following properties:

  • isDefault
  • isPending
  • isResolved
  • text // default, pending, resolved, rejected.
  • disabled // will be false when valid or is disableDuringSubmit attr is added to the validated-form and the submit is pending.
  1. errors

  2. validation a contextual component you can use to localize error handling.

All this allows you to do something like this in your route:

actions: {
  submit(reset, resolve, reject) {
    const self = this;
    self.get('service').submit({
      firstName: self.controller.get('firstName'),
      lastName: self.controller.get('lastName'),
      age: self.controller.get('age'),
    })
    .then(function() {
      reset();
      resolve();
    })
    .catch(function(errors) {
      reject({
        firstName: errors.firstName, //['first name is invalid!']
        lastName: errors.lastName,
        age: errors.age,
      });
    });
  }
}

Errors are then yielded by the validated-form and passed to each of the individual form.validation components.

  {{#validated-form as |form|}}
    {{#form.validation submitErrors=form.errors.firstName}}
      <input type='text' value={{firstName}}/>
    {{/form.validation}}
  {{/validated-form}}

form.validated Component

The form.validated component yields the following:

  1. check an action which will ignore errors and only change the state when it passes the validation.

  2. validate an action which will check for both success and errors.

  3. errors an array of error messages.

  4. state object with the following properties:

  • valid // null, true, false
  • isValid // true, false
  • isInvalid // true, false
  • isInitial // true, false
  • text // valid, invalid, initial

Helpers

Stickler provides a for-bool helper for managing error classes. It takes valid as the first param and then three classes.

  • first for true
  • second for false
  • third for null (defaults to an empty string)
  <div class="form-group {{for-bool validation.state.valid 'has-success' 'has-error'}}">

Stickler also provides a first helper which you can use to get the first error message from errors

{{#if validation.errors}}
  <span>{{first validation.errors}}</span>
{{/if}}

And a join helper which you can use when you need to combine strings with a separator (it's first arg).

{{input
  class=(join ' ' 'class-a' 'class-b' 'class-c')
}}

Default Validations

Email

Rule: email

Attrs: emailMessage

Required

Rule: required

Attrs: emailMessage

Min Length

Rule: min-length

Attrs: minLengthMessage, minLength

Max Length

Rule: max-length

Attrs: maxLengthMessage, maxLength

Exists

Rule: exists

Attrs: existsMessage

Url

Rule: url

Attrs: urlMessage

Digits

Rule: digits

Attrs: digitsMessage

Number

Rule: number

Attrs: numberMessage

Date

Rule: date

Attrs: dateMessage

DateISO

Rule: dateISO

Attrs: dateISOMessage

Credit Card

Rule: credit-card

Attrs: creditCardMessage

Max

Rule: max

Attrs: maxMessage

Min

Rule: min

Attrs: minMessage

Format

Rule: format

Attrs: formatMessage, format

Transforms

  • trim
  • digit

You can add your own rules and transforms.

generate a transform

ember g stickler-transform <name>

generate a rule

ember g stickler-rule <name>

Authors

License

The MIT License

Copyright (c) 2015-2016 sethpollack, runspired

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.