2.0.2 • Published 3 years ago

ng-absolute-validator v2.0.2

Weekly downloads
4
License
MIT
Repository
-
Last release
3 years ago

Angular Absolute Validator

Like no other form validation library, simply write in English your requirements inside your form HTML tags, Abosolute validator will does the rest.

New Features

  • No more add field name for reactive validation .
  • Now add error message with multiple language.
  • Set fallback language for empty main message
  • Add language and message object in application root.
  • Add inline error message with multi language support.

Dependencies

This Library requires moment to be installed.

Documentation

Go to Full Documentation

Installation

To install this library, run:

$ npm install ng-absolute-validator

Import FormsModule and NgAbsoluteValidatorModule in your Angular AppModule:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { NgAbsoluteValidatorModule } from 'ng-absolute-validator';

@NgModule({
	declarations: [
		AppComponent
	],
	imports: [
		BrowserModule,
		FormsModule,
		NgAbsoluteValidatorModule
	],
	providers: [],
	bootstrap: [AppComponent]
})
export class AppModule { }

Add CSS Style for color effect on validation

Add CSS style to application style.css file to get color effect on validation.

.ng-invalid:not(form):not(.ng-untouched){
    border: 1px solid red;
}
.ng-valid:not(form){
    border: 1px solid green;
}

Template driven form validation Example

Once your library is imported, you can use validation display message component and directive to activate validation process.

<!-- For directive use -->
<form (ngSubmit)="onSubmit(form)" #form="ngForm">
	<input [above]="<number>" type="text" name="<name>" [(ngModel)]="<name>" #<name>="ngModel">
    <ng-absolute-validator  [formInstance]="<name>" (onValid)="getStatus($event)"></ng-absolute-validator>
</form>

<!-- For Chain validation -->
<form (ngSubmit)="onSubmit(form)" #form="ngForm">
	<input [Rule]="required|integer|above:20" type="text" name="<name>" [(ngModel)]="<name>" #<name>="ngModel">
	<ng-absolute-validator  [formInstance]="<name>" (onValid)="getStatus($event)"></ng-absolute-validator>    
</form>

Reactive form validation Example

import { Component, Input } from '@angular/core';
import { ReactiveValidator } from '@package/services/reactive.validate';
import { FormBuilder, FormGroup } from '@angular/forms';

@Component({
    selector: 'app-reactive',
    templateUrl: './reactive.component.html',
    styleUrls: ['./reactive.component.css']
})
export class ReactiveComponent{

	public form  : FormGroup;

  	constructor(
      	private fb : FormBuilder,
		private rv : ReactiveValidator,
	){this.generteForm()}
	
	public aboveMessage : any = {
		en:{above:'The :attribute should be above :arg0.'},
		ar:{above:'ال :attribute يجب أن يكون أعلاه :arg0.'}
	}

	public formRules  = {
		above			: ['',this.rv.map('above:20',this.aboveMessage)],
		dimension       : ['',
			this.rv.map('image'),
			this.rv.dimension('width=200px,height=200px')
		],
		username    	: ['',
			this.rv.map('string'),
			this.rv.exists('http://jsonplaceholder.typicode.com/users,GET')
		],
		email    	    : ['',
			this.rv.map('email'),
			this.rv.unique('http://jsonplaceholder.typicode.com/users,GET')
		],
	}

  	generteForm(){
		this.form = this.fb.group(this.formRules)
	}
}                                                              
                                                                                    

Validation Rules

All validation rules and error message details are given on documentation Full Documentation

RulesDescription
aboveMakes sure the value provided by the end user is above the expected value. This method will wrapboth the values
acceptedEnsures that the field under validation is accepted. Empty strings, false, null, 0 and undefinedvalues will be considered as not accepted.
afterEnsures the value of the field is after the expected date.
alphaMakes sure the field under validation is alpha only.
alphaDashMakes sure the field under validation is contain letters, numbers, dashes and underscores.
alphaNumericMakes sure the field under validation is alpha numeric only.
arrayEnsure the value is a valid array. Also this validation will never validate the size of array.
beforeEnsures the value of field under validation is before a given date.
booleanEnsures the value of a field is a boolean. Also it will cast following strings to their booleancounter parts.
confirmedEnsures a field value as confirmed using a _confirmation convention. This is mainly used forpassword confirmation field.For example: If the password field name is password, then another field called password_confirmationmust exist and should have the same value as the actual field.
creditCardEnsures a field value must be a valid credit card number.
cvvEnsures a field value must be a CVV number.
dateEnsures the field under validation is a valid date. The value can be a date object or a valid datestring.
dateFormatEnsures the date or date time is valid as the one of the defined formats.
debitCardEnsures a field value must be a valid debit card number.
differentEnsures the value of the field under validation is always different from the targeted field value.
dimensionEnsures the The file under validation must be an image meeting the dimension constraints asspecified by the accppeted params like: height=200,max_height=200,width=200,max_width=200,ratio=1.2
emailEnsures the field under validation is a valid email format.
endsWithEnsure the value of field under validation ends with a certain substr. This validation will alsotrim whitespaces before making the check
existsEnsures the value is exists in database GET,POST,PUT,PATCH method isallowed for rmote validation. default method is GET.
imageEnsures the upload file is an valid image file
inEnsures the value of a given field matches one of expected values.
includesEnsures the value of field under validation contains a given substring.
integerEnsures the value is a valid integer. Also string representation of a number will return true.
ipEnsures the value is a valid ip address as per ipv4 and ipv6 specs.
ipv4Ensures the value is a valid ip address as per ipv4 spec only.
ipv6Ensures the value is a valid ip address as per ipv6 spec only.
jsonEnsures the value of field under validation is safe to be parsed using JSON.parse method.
maxEnsures the length of a string or array or number is not greater than the defined length.
minEnsures the length of a string or array or number is not is not less than the expected length
mimesEnsures the file must have a MIME type corresponding to one of the listed extensions.
notEqualsMakes sure that the value of field under validation is not same as the defined value.
notInMakes sure that the value of field under validation is not from one of the defined values.
notRegexEnsures the value of field under validation, passes the regex test. The regex can be defined as astring or a RegExp object.
numberMakes sure that the value of field under validation is a valid number. The validation will pass forfloats too, since it uses typeof internally.
phoneNoMakes sure that the value of field under validation is a valid phone number. In default phone numberis validate more than 10 Style of number.To override the default behaviour pass new regex expressionto validate the number.
rangeEnsures the value of field under validation is under a given range. The values will be cased toNumber automatically.
regexEnsures the value of field under validation, passes the regex test. The regex can be defined as astring or a RegExp object.
requiredEnsures the value of field under validation is not empty.
requiredIfEnsures the field under validation must be present and not empty if the anotherfield field is equalto any value. The Match with field must be define or initialized or placed before validated field.
requiredUnlessEnsures the field under validation must be present and not empty unless the anotherfield field isequal to any value. The Match with fields must be define or initialized or placed before validatedfield.
requiredWithEnsure The field under validation must be present and not empty only if any of the other specifiedfields are present. The Match with fields must be define or initialized or placed before validatedfield.
requiredWithAllEnsure The field under validation must be present and not empty only if all of the other specifiedfields are present. The Match with fields must be define or initialized or placed before validatedfield.
requiredWithoutEnsure The field under validation must be present and not empty only when any of the other specifiedfields are not present. The Match with fields must be define or initialized or placed beforevalidated field.
requiredWithoutAllEnsure The field under validation must be present and not empty only when all of the other specifiedfields are not present. The Match with fields must be define or initialized or placed beforevalidated field.
sameEnsures the value of 2 fields are same.
sizeEnsures the size of the file not more than the specific size (in KB).
startsWithEnsures the value of 2 fields are same.
stringEnsures the value is a string.
strengthMake sure the value strength should mach pre defined regex expression. Add or overide with new regex expression from root config. Add or modify progess bar color combination from root config. a : String must be 8 charecter long. b : String must contain one uppercase letter. c : String must contain one spatial charecter. d : String must contain one digit. b : String must contain one lowecase letter.
underEnsures the value of a field is under a certain value. All values will be casted to Number
urlEnsures the value is a valid URL format.
uuidEnsures the value is a valid UUID format.
uniqueEnsures the value is unique in database GET,POST,PUT,PATCH method isallowed for rmote validation. default method is GET.
videoEnsures the upload file is an valid video file

License

This project is licensed under the terms of the MIT license.

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

4 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago