How to trigger validation on reactive form through input decorator?
up vote
0
down vote
favorite
I have a validation directive that takes an input from a parent component. The validation is passed only when the input is true. So whenever an input changes, I don't know how to trigger validation method in the directive
I have a sample working code on stackblitz for the purpose of demonstrating the issue
//As you can see the appRouteValidator is passing an input randomNumber
<input type="random" appRouteValidator [randomOption]="randomNumber"
formControlName="random" class="form-control"/>
The validate method here is I want to call on every time the input changes,
export class DemoValidatorDirective {
@Input() randomOption: Boolean;
ngOnChanges(changes: SimpleChanges) {
if (changes.randomOption.currentValue) {
//---------------------------------> Should trigger validate method
}
}
constructor() { }
// This should be called when it receives an input
validate(control: AbstractControl): { [key: string]: any; } {
console.log('validation method called');
if (control && (control.value !== null || control.value !== undefined)) {
const randomField = control.value;
if (!randomField || !this.randomOption) {
return {
'validationError': 'invalid random number'
};
}
}
return null;
}
}
angular angular-reactive-forms
add a comment |
up vote
0
down vote
favorite
I have a validation directive that takes an input from a parent component. The validation is passed only when the input is true. So whenever an input changes, I don't know how to trigger validation method in the directive
I have a sample working code on stackblitz for the purpose of demonstrating the issue
//As you can see the appRouteValidator is passing an input randomNumber
<input type="random" appRouteValidator [randomOption]="randomNumber"
formControlName="random" class="form-control"/>
The validate method here is I want to call on every time the input changes,
export class DemoValidatorDirective {
@Input() randomOption: Boolean;
ngOnChanges(changes: SimpleChanges) {
if (changes.randomOption.currentValue) {
//---------------------------------> Should trigger validate method
}
}
constructor() { }
// This should be called when it receives an input
validate(control: AbstractControl): { [key: string]: any; } {
console.log('validation method called');
if (control && (control.value !== null || control.value !== undefined)) {
const randomField = control.value;
if (!randomField || !this.randomOption) {
return {
'validationError': 'invalid random number'
};
}
}
return null;
}
}
angular angular-reactive-forms
input is true means?
– Ajay Ojha
Nov 10 at 13:15
@Input() randomOption: Boolean; it receives a boolean.
– anonymous
Nov 10 at 13:16
Ok, please correct me if I am wrong, based on your randomOption value you want to validate the 'random' control? if the value is true then it should validate otherwise not.
– Ajay Ojha
Nov 10 at 13:36
Question is I want to trigger the validate method when the directive receives the input.
– anonymous
Nov 10 at 14:03
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a validation directive that takes an input from a parent component. The validation is passed only when the input is true. So whenever an input changes, I don't know how to trigger validation method in the directive
I have a sample working code on stackblitz for the purpose of demonstrating the issue
//As you can see the appRouteValidator is passing an input randomNumber
<input type="random" appRouteValidator [randomOption]="randomNumber"
formControlName="random" class="form-control"/>
The validate method here is I want to call on every time the input changes,
export class DemoValidatorDirective {
@Input() randomOption: Boolean;
ngOnChanges(changes: SimpleChanges) {
if (changes.randomOption.currentValue) {
//---------------------------------> Should trigger validate method
}
}
constructor() { }
// This should be called when it receives an input
validate(control: AbstractControl): { [key: string]: any; } {
console.log('validation method called');
if (control && (control.value !== null || control.value !== undefined)) {
const randomField = control.value;
if (!randomField || !this.randomOption) {
return {
'validationError': 'invalid random number'
};
}
}
return null;
}
}
angular angular-reactive-forms
I have a validation directive that takes an input from a parent component. The validation is passed only when the input is true. So whenever an input changes, I don't know how to trigger validation method in the directive
I have a sample working code on stackblitz for the purpose of demonstrating the issue
//As you can see the appRouteValidator is passing an input randomNumber
<input type="random" appRouteValidator [randomOption]="randomNumber"
formControlName="random" class="form-control"/>
The validate method here is I want to call on every time the input changes,
export class DemoValidatorDirective {
@Input() randomOption: Boolean;
ngOnChanges(changes: SimpleChanges) {
if (changes.randomOption.currentValue) {
//---------------------------------> Should trigger validate method
}
}
constructor() { }
// This should be called when it receives an input
validate(control: AbstractControl): { [key: string]: any; } {
console.log('validation method called');
if (control && (control.value !== null || control.value !== undefined)) {
const randomField = control.value;
if (!randomField || !this.randomOption) {
return {
'validationError': 'invalid random number'
};
}
}
return null;
}
}
angular angular-reactive-forms
angular angular-reactive-forms
asked Nov 10 at 12:56
anonymous
185115
185115
input is true means?
– Ajay Ojha
Nov 10 at 13:15
@Input() randomOption: Boolean; it receives a boolean.
– anonymous
Nov 10 at 13:16
Ok, please correct me if I am wrong, based on your randomOption value you want to validate the 'random' control? if the value is true then it should validate otherwise not.
– Ajay Ojha
Nov 10 at 13:36
Question is I want to trigger the validate method when the directive receives the input.
– anonymous
Nov 10 at 14:03
add a comment |
input is true means?
– Ajay Ojha
Nov 10 at 13:15
@Input() randomOption: Boolean; it receives a boolean.
– anonymous
Nov 10 at 13:16
Ok, please correct me if I am wrong, based on your randomOption value you want to validate the 'random' control? if the value is true then it should validate otherwise not.
– Ajay Ojha
Nov 10 at 13:36
Question is I want to trigger the validate method when the directive receives the input.
– anonymous
Nov 10 at 14:03
input is true means?
– Ajay Ojha
Nov 10 at 13:15
input is true means?
– Ajay Ojha
Nov 10 at 13:15
@Input() randomOption: Boolean; it receives a boolean.
– anonymous
Nov 10 at 13:16
@Input() randomOption: Boolean; it receives a boolean.
– anonymous
Nov 10 at 13:16
Ok, please correct me if I am wrong, based on your randomOption value you want to validate the 'random' control? if the value is true then it should validate otherwise not.
– Ajay Ojha
Nov 10 at 13:36
Ok, please correct me if I am wrong, based on your randomOption value you want to validate the 'random' control? if the value is true then it should validate otherwise not.
– Ajay Ojha
Nov 10 at 13:36
Question is I want to trigger the validate method when the directive receives the input.
– anonymous
Nov 10 at 14:03
Question is I want to trigger the validate method when the directive receives the input.
– anonymous
Nov 10 at 14:03
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
for manually calling validation method of Respective FormControl, you need call the AbstractControl class method of 'updateValueAndValidity' based on your validation rules.
I have added two lines in the generateRandomNumber method, here is the code:
generateRandomNumber() {
let randomNumber: any = Math.floor(Math.random() * 2) + 1;
this.randomNumber = randomNumber % 2 === 0;
if(this.randomNumber)
this.registerForm.controls.random.updateValueAndValidity();
console.log('random number', this.randomNumber);
}
Please refer the working example on stackblitz
Please let me know if you have any question.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
for manually calling validation method of Respective FormControl, you need call the AbstractControl class method of 'updateValueAndValidity' based on your validation rules.
I have added two lines in the generateRandomNumber method, here is the code:
generateRandomNumber() {
let randomNumber: any = Math.floor(Math.random() * 2) + 1;
this.randomNumber = randomNumber % 2 === 0;
if(this.randomNumber)
this.registerForm.controls.random.updateValueAndValidity();
console.log('random number', this.randomNumber);
}
Please refer the working example on stackblitz
Please let me know if you have any question.
add a comment |
up vote
0
down vote
for manually calling validation method of Respective FormControl, you need call the AbstractControl class method of 'updateValueAndValidity' based on your validation rules.
I have added two lines in the generateRandomNumber method, here is the code:
generateRandomNumber() {
let randomNumber: any = Math.floor(Math.random() * 2) + 1;
this.randomNumber = randomNumber % 2 === 0;
if(this.randomNumber)
this.registerForm.controls.random.updateValueAndValidity();
console.log('random number', this.randomNumber);
}
Please refer the working example on stackblitz
Please let me know if you have any question.
add a comment |
up vote
0
down vote
up vote
0
down vote
for manually calling validation method of Respective FormControl, you need call the AbstractControl class method of 'updateValueAndValidity' based on your validation rules.
I have added two lines in the generateRandomNumber method, here is the code:
generateRandomNumber() {
let randomNumber: any = Math.floor(Math.random() * 2) + 1;
this.randomNumber = randomNumber % 2 === 0;
if(this.randomNumber)
this.registerForm.controls.random.updateValueAndValidity();
console.log('random number', this.randomNumber);
}
Please refer the working example on stackblitz
Please let me know if you have any question.
for manually calling validation method of Respective FormControl, you need call the AbstractControl class method of 'updateValueAndValidity' based on your validation rules.
I have added two lines in the generateRandomNumber method, here is the code:
generateRandomNumber() {
let randomNumber: any = Math.floor(Math.random() * 2) + 1;
this.randomNumber = randomNumber % 2 === 0;
if(this.randomNumber)
this.registerForm.controls.random.updateValueAndValidity();
console.log('random number', this.randomNumber);
}
Please refer the working example on stackblitz
Please let me know if you have any question.
answered Nov 10 at 14:30
Ajay Ojha
93027
93027
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239178%2fhow-to-trigger-validation-on-reactive-form-through-input-decorator%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
input is true means?
– Ajay Ojha
Nov 10 at 13:15
@Input() randomOption: Boolean; it receives a boolean.
– anonymous
Nov 10 at 13:16
Ok, please correct me if I am wrong, based on your randomOption value you want to validate the 'random' control? if the value is true then it should validate otherwise not.
– Ajay Ojha
Nov 10 at 13:36
Question is I want to trigger the validate method when the directive receives the input.
– anonymous
Nov 10 at 14:03