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-changeset-cp-validations

v0.2.0

Published

Ember CP Validations support for Ember Changeset

Downloads

3,874

Readme

Ember Changeset CP Validations

Build Status npm version Code Climate Test Coverage Dependency Status

Ember CP Validations support for Ember Changeset

Requirements

Installation

ember install ember-changeset-cp-validations

Helpful Links

Looking for help?

If it is a bug please open an issue on GitHub.

Usage

The Template Helper

This addon updates the changeset helper by creating a changeset instance via the createChangeset.

{{dummy-form
    changeset=(changeset user)
    submit=(action "submit")
    rollback=(action "rollback")
}}

Passing a custom action as a second argument to the changeset helper is supported but make sure to call the passed validate method to run the necessary validations.

{{dummy-form
    changeset=(changeset user (action "customValidate"))
    submit=(action "submit")
    rollback=(action "rollback")
}}
import Ember from 'ember';

const { Component } = Ember;

export default Component.extend({
  actions: {
    customValidate({ key, newValue, oldValue, changes }, validate) {
      // do some custom stuff

      return validate(...arguments);
    }
  }
});

Creating a Changeset

There are 2 ways to create a changset programmatically.

Using createChangeset

createChangeset is a no fuss way of quickly creating a new changeset instance. It will return a new changeset instance that is setup for the passed model and its validations.

import Ember from 'ember';
import createChangeset from 'ember-changeset-cp-validations';

const { Component } = Ember;

export default Component.extend({
  init() {
    this._super(...arguments);

    let model = get(this, 'model');
    this.changeset = createChangeset(model);
  }
});

Using buildChangeset

buildChangeset allows you more freedom with creating the changeset instance. It will return a hash that includes the validateFn and validationMap required to support the validations for the passed model.

import Ember from 'ember';
import { buildChangeset } from 'ember-changeset-cp-validations';

const { Component } = Ember;

export default Component.extend({
  init() {
    this._super(...arguments);

    let model = get(this, 'model');
    let { validateFn, validationMap } = buildChangeset(model);
    this.changeset = new Changeset(model, validateFn, validationMap);
  }
});

Disable CP Validations Dependents

When changes get applied to the actual model, validations will be re-triggered since the dependents of the CPs have changed. As of ember-cp-validations v3.1.0, to disable this, you may put all or selective attribute CPs in a volatile state via the volatile option.

const Validations = buildValidations({
  username: {
    description: 'Username',
    validators: [
      validator('presence', true),
      validator('length', {
        min: 5,
        max: 15
      })
    ]
  },

  password: {
    description: 'Password',
    validators: [
      validator('presence', true),
      validator('length', {
        min: 4,
        max: 10,
        volatile: false
      })
    ]
  }
}, {
  volatile: true
});