Unique
The Unique rule performs remote validation, sending the field's value to a backend endpoint to confirm no existing record already holds it — the direct opposite of the Exists rule. It's the standard check for a signup form's username or email field, where the value needs to be free rather than already on file, catching a taken username or a duplicate account before the form is ever submitted. Both GET and POST are supported for the request, with GET used by default, and like Exists, it's asynchronous by nature and isn't available through the pipe-delimited chain-rule syntax — it needs the Template Driven or Reactive binding modes, where an async validator can be wired in properly.
#Directive Code Snippet
<!DOCTYPE html>
<html lang="en">
...
<form (ngSubmit)="onSubmit(form)" #form="ngForm">
<input [unique]="{url:<API_URL>,method:<method>}" type="text" name="<name>" [(ngModel)]="<name>" #<name>="ngModel">
<ng-absolute-validator [formInstance]="<name1>" [loader]="true"></ng-absolute-validator>
</form>
...
</html>
#Chain Rule Code Snippet
Exists Validation does not support yet.
#Reactive Form Code Snippet
//component.ts
export class ReactiveComponent{
public form !: FormGroup;
constructor(
private fb : FormBuilder,
private rv : ReactiveValidator,
){this.generteForm()}
generteForm(){
this.form = this.fb.group({
<name> : ['',[],[this.rv.unique({url:'http://jsonplaceholder.typicode.com/users'},<CUSTOM_MESSAGE>)]],
})
}
}
//component.html
<!DOCTYPE html>
<html lang="en">
...
<form [formGroup]="form">
<input type="text" name="<name>" formControlName="<name>">
<ng-absolute-validator [formInstance]="form.get(<name>)" [loader]="true"></ng-absolute-validator>
</form>
...
</html>
#Config Options:
| Property | Type | Required | Description |
|---|---|---|---|
url |
string |
Yes | The endpoint URL to which the HTTP request will be sent. |
field |
string |
No | The specific field/property name to be used or mapped from the response. by default it will be the form name |
request |
object |
No | The request body/payload to be sent with the HTTP call. Typically used with POST requests. |
params |
object |
No | Query parameters to be appended to the URL (used with GET requests or additional filters). |
method |
'GET' | 'POST' |
No | The HTTP method to use for the request. Defaults to 'GET' if not specified. |