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

ng-conditionally-validate

v1.0.2

Published

NG Conditionally Validate

Downloads

27

Readme

Build Status

Ng-Conditionally-Validate

An angular 4 library that aims to improve the functionality of your forms without adding any of the complexity.

Install

npm i ng-conditionally-validate --save

Hook into your module

import { ConditionallyValidateModule } from 'ng-conditionally-validate';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';

@NgModule({
  ...
  imports: [
    ...
    NgModule,
    ReactiveFormsModule,
    ConditionallyValidateModule,
    ...
  ],
  ...
})
export class AppModule { }

Inject Service into your Component

import { Component } from '@angular/core';
import { ConditionallyValidateService } from 'ng-conditionally-validate';

@Component({ ... })
export class ExampleComponent {

  constructor(private cv: ConditionallyValidateService, ...) { ... }

}

How To

Reactive Forms

This library takes advantage of angular's reactive forms module. If you are making forms of any kind inside your application and you are not using it, you should take the time to read up on what it is and how to get it setup in your project. It will speed up making any kind of form you can think of, and provides many different methods of form validation right in their with it while making it easy to implement your own validators. On top of all that, it transforms all form edits by the user into ReactiveX observables, giving you the power of any observable operator.

Creating a Form

First we want to create a basic reactive form to hook into the template of our controller. I will not go into too much detail for this part of the process since there are plenty tutorials out there that tells you how it works in great detail. So in our component we might have something like this:

import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component(...)
export class Example  {

  form: FormGroup;

  constructor(
    private fb: FormBuilder
  ) {

    this.form = fb.group({
      referal: ['', Validators.required],
      alternativeReferal: ''
    });

  }

}

Here we've used Angular's Form Builder to create us a reactive form with the two controls referal and alternativeReferal. At this point in time the form has a single validator function that is run against the value of referal whenever some change to the form is made. With this specific validator, referal must not be an empty string for it to be considered valid. Once all controls are considered valid then the form as a whole is considered valid.

This kind of form might seem familiar. Imagine we're trying to figure out how people found out about your online service. You might have a dropdown who's value is bound to the referal control of our form. But of course you can't think of every posibility of how a user found your website, so you've left an extra option 'other', and a extra field alternativeReferal for a user to fill in. You don't really need the user to fill in the alternative referal field unless the referal field is set to 'other'. Also, you really don't want the alternative referal text field to even render unless needed. This library makes that kind of functionality easy.

Enter: Ng Conditionally Validate

All we need to do is import the Conditionally Validate Service and inject it into our component. We pass it our form and specify what controls we want to have some form of validation ran on them using the service's validate() function. This function is meant to be chained with multiple other calls to specify what exactly you want to happen with validation and how. At the end of the chaining you are returned an Rx observable that emits a boolean as to whether or not your validation functions you specified are being ran on the form controls you passed in. In the end we have our controller looking something like this:

import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ConditionallyValidateService } from 'ng-conditionally-validate';
import { Observable } from 'rxjs/Rx';

@Component(...)
export class Example  {

  form: FormGroup;
  displayAlternative$: Observable<boolean>;

  constructor(
    private cv: ConditionallyValidateService,
    private fb: FormBuilder
  ) {

    this.form = fb.group({
      referal: ['', Validators.required],
      alternativeReferal: ''
    });

    this.displayAlternative$ = cv
      .validate(this.form, 'alternativeReferal')
      .using(Validators.required)
      .when('referal')
      .is('other');
  }

}

And the template code might look like this:

<h1>How'd you hear about us?</h1>
<div [formGroup]="form">
  <select formControlName="referal">
    <option value="ghub">Github</option>
    <option value="twitter">Twitter</option>
    <option value="other">Other</option>
  </select>
  <div *ngIf="displayAlternative$ | async">
    <p>Please enter how you heard about us</p>
    <input formControlName="alternativeReferal">
  </div>
</div>

The form is now only considered valid as a whole when referal is not empty, and if it is 'other' then alternative referal must not be empty.