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

angular-conditional-validation

v0.1.2

Published

AngularJS module that enables conditional validation

Downloads

85

Readme

Build Status

Angular conditional validation module

The conditional validation module provides an enableValidation attribute directive that can be used to conditionally enable/disable validation for elements with an ngModel directive.

Motivation

Most (if not all) of the standard Angular validators can be disabled individually using falsy values. For example when the expression for a ngMin attribute evaluates to undefined, then validation will be disabled for the min validator. However, this requires that the logic that determines when validation should be enabled is combined with the logic that defines the values for parameterized validators. At best you can write something like this:

<input
  type="date"
  ng-model="myCtrl.startDate"
  ng-min="myCtrl.enableStartDateValidation && myCtrl.minStartDate">

The enableValidation directive provides a cleaner method to separate the logic. When applied to the example above, the result looks like this:

<input type="date"
  ng-model="myCtrl.startDate"
  ng-min="myCtrl.minStartDate"
  enable-validation="myCtrl.enableStartDateValidation">

Using the enableValidation directive it also easier to disable multiple validators at once. If the example above is extended with a max validator, you can simply add the ng-max attribute without having to repeat myCtrl.enableStartDateValidation && in the validator expression. The result:

<input type="date"
  ng-model="myCtrl.startDate"
  ng-min="myCtrl.minStartDate"
  ng-max="myCtrl.maxStartDate"
  enable-validation="myCtrl.enableStartDateValidation">

Another reason for using the enableValidation directive is when you use (custom) validators that do not provide a method to (conditionally) disable them.

Installation

NPM

npm install angular-conditional-validation

Bower

bower install angular-conditional-validation

Usage

To use the Angular conditional validation module in your application either include the script (angular-conditional-validation(.min).js) using a <script> tag or require/load it via a script loader. Furthermore add angularConditionalValidation to the Angular dependencies of your application.

Now you should be able to use the enableValidation directive in your templates.

The directive supports three types of values:

  • Objects: When object values are used to enable/disable validation for individual validators. The property keys of the object should match with the name of the validators. The value of a property (which may be a function) determines whether validation is enabled/disabled.
  • Functions: Functions values will be invoked every time validation is triggered and the return value (which may be an object) is then used to determine whether validation is enabled or disabled.
  • Other: Any other value type will be evaluated as an inherent boolean value, so any truthy value enables validation while falsy values disable validation.

Both synchronous and asynchronous validators are supported by the enableValidation directive.

Examples

A set of demos can be found at the following JS Bin: jsbin.com/necile

Simple boolean validation condition

var ctrl = this;
ctrl.someValue = 'hello';
ctrl.anotherValue = true;
<input type="text" ng-model="ctrl.someValue" ng-minlength="5" enable-validation="ctrl.anotherValue">

Function based validation condition

var ctrl = this;
ctrl.someValue = 'hello';
ctrl.shouldValidate = function() {
  return new Date().getDay() == 1; // Only validate on mondays :)
};
<input type="text" ng-model="ctrl.someValue" ng-minlength="5" enable-validation="ctrl.shouldValidate">

Object based validation condition

var ctrl = this;
ctrl.someValue = 'hello';
ctrl.enabledValidators = {
  required: false,
  minlength: true,
  '*': false // Disable other validators.
};
<input
  type="text"
  ng-model="ctrl.someValue"
  required
  ng-minlength="4"
  ng-maxlength="6"
  pattern="abcde"
  enable-validation="ctrl.enabledValidators">

Mixing functions and objects

var ctrl = this;
ctrl.someValue = 'hello';
ctrl.enabledValidators = function() {
  return {
    required: false,
    minlength: true,
    '*': function() {
      return new Date().getDay() == 2; // Other validators are only enabled on tuesday.
    }
  };
};
<input
  type="text"
  ng-model="ctrl.someValue"
  required
  ng-minlength="4"
  ng-maxlength="6"
  pattern="abcde"
  enable-validation="ctrl.enabledValidators">

Limitations

  • Promises are not supported.