Hiển thị các bài đăng có nhãn custom validator. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn custom validator. Hiển thị tất cả bài đăng

Angular 2 Custom Validator Directive for Whitespace and Empty Strings

Source: https://www.jokecamp.com/blog/angular-whitespace-validator-directive/

Create an Angular 2 Custom Validator Directive for Whitespace and Empty Strings


How do we create a custom validator directive to treat whitespace as invalid in Angular (Angular 2)? The built in angular required input is easily fooled by a run of spaces so lets create our own validator. I read the Angular.io Custom Validator docs for you.
We can use this rather simple idea to demonstrate a validator with unit testing, wrapped in a simple directive.
Jump to the code:

Custom Validator Function for Whitespace

The tricky part here is returning a ValidatorFn function that returns null on valid state and a {key: value} on an invalid state.
The no-whitespace.validator.ts file:
import { AbstractControl, ValidatorFn } from '@angular/forms';

export function NoWhitespaceValidator(): ValidatorFn {

  return (control: AbstractControl): { [key: string]: any } => {

     // messy but you get the idea
    let isWhitespace = (control.value || '').trim().length === 0;
    let isValid = !isWhitespace;
    return isValid ? null : { 'whitespace': 'value is only whitespace' }

  };
}

How to Write Tests for our Custom Validator Function

The no-whitespace.validator.spec.ts file using Jasmine.
import { AbstractControl } from '@angular/forms';
import { NoWhitespaceValidator } from './no-whitespace.validator';

describe('Whitespace Validator', () => {

  let validatorFn = NoWhitespaceValidator();

  it('empty string is invalid', () => {
    let control = { value: '' }
    let result = validatorFn(control as AbstractControl)
    expect(result !== null).toBe(true);
    expect(result['whitespace']).toBe('value is only whitespace')
  });

  it('spaces only are invalid', () => {
    let control = { value: '    ' }
    let result = validatorFn(control as AbstractControl)
    expect(result !== null).toBe(true);
  });

  it('null is invalid', () => {
    let control: any = { value: null }
    let result = validatorFn(control as AbstractControl)
    expect(result !== null).toBe(true);
  });

  it('text is not considered invalid', () => {
    let control = { value: 'i have whitespace on the inside' }
    let result = validatorFn(control as AbstractControl)
    expect(result).toBe(null);
  });

});

Directive for our Custom Validator function

We can wrap our validator in a simple directive for use as an HTML attribute.
  • it is best practice to prefix your own directives. I used my for myNoSpaces
  • be sure you are providing NG_VALIDATORS in the directive providers array. Nothing will happen without it.
The no-whitespace.directive.ts file:
import { Directive, Input, OnChanges, SimpleChanges } from '@angular/core';
import { Validator, AbstractControl, Validators, NG_VALIDATORS } from '@angular/forms';

import { NoWhitespaceValidator } from './no-whitespace.validator';

/**
 * This validator works like "required" but it does not allow whitespace either
 *
 * @export
 * @class NoWhitespaceDirective
 * @implements {Validator}
 */
@Directive({
    selector: '[myNoSpaces]',
    providers: [{ provide: NG_VALIDATORS, useExisting: NoWhitespaceDirective, multi: true }]
})
export class NoWhitespaceDirective implements Validator {

    private valFn = NoWhitespaceValidator();
    validate(control: AbstractControl): { [key: string]: any } {
        return this.valFn(control);
    }
}

Example Usage:

in HTML

  • Be sure to include a reference to the directive in the necessary module declarations array.
  • It can be used in substitution of required because it also treats an empty string as invalid.
<input type="text" id="name" myNoSpaces name="name" [(ngModel)]="hero.name" #name="ngModel">

in code

this.jobForm = this.fb.group({
jobTitle: ['', NoWhitespaceValidator()]
});

Resources

Angular CustomValidators: Password example

Source: https://codinglatte.com/posts/angular/cool-password-validation-angular/

import { ValidationErrors, ValidatorFn, AbstractControl } from '@angular/forms';

export class CustomValidators {
static patternValidator(regex: RegExp, error: ValidationErrors): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } => {
if (!control.value) {
// if control is empty return no error
return null;
}

// test the value of the control against the regexp supplied
const valid = regex.test(control.value);

// if true, return no error (no error), else return error passed in the second parameter
return valid ? null : error;
};
}

static passwordMatchValidator(control: AbstractControl) {
const password: string = control.get('newPassword').value; // get password from our password form control
const confirmPassword: string = control.get('retypePassword').value; // get password from our confirmPassword form control
// compare is the password math
if (password !== confirmPassword) {
// if they don't match, set an error in our confirmPassword form control
control.get('retypePassword').setErrors({ NoPassswordMatch: true });
}
}
}

public static getPasswordValidators() {
return Validators.compose([
Validators.required,
// check whether the entered password has a number
CustomValidators.patternValidator(/\d/, {
hasNumber: true
}),
// check whether the entered password has upper case letter
CustomValidators.patternValidator(/[A-Z]/, {
hasCapitalCase: true
}),
// check whether the entered password has a lower case letter
CustomValidators.patternValidator(/[a-z]/, {
hasSmallCase: true
}),
// check whether the entered password has a special character
CustomValidators.patternValidator(
/[ !@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/,
{
hasSpecialCharacters: true
}
),
Validators.minLength(8),
Validators.maxLength(50)
])
}


createPasswordForm(): FormGroup {
return this._fb.group(
{
currentPassword: [
null,
Validators.compose([Validators.required])
],
newPassword: [
null,
Utils.getPasswordValidators()
],
retypePassword: [null, Validators.compose([Validators.required])]
},
{
// check whether our password and confirm password match
validator: CustomValidators.passwordMatchValidator
}
);
}

Renewing Facebook Graph API token automatically?

  Mã truy cập dài hạn https://developers.facebook.com/docs/facebook-login/guides/access-tokens/get-long-lived/ https://community.n8n.io/t/re...