Any simple way to apply one validation rule to all fields by Angular 6
up vote
-1
down vote
favorite
Consider a data model contains 20 string fields with different name ("name", "address", "company".. etc ), mapped to Angular front end as 20 input fields. Now the requirement asks to validate each fields that prevent user submit the form if any fields contain special characters.
I can work out the regex pattern but just wondering any simple way to do one validator to all fields.
javascript angular angular-validation
add a comment |
up vote
-1
down vote
favorite
Consider a data model contains 20 string fields with different name ("name", "address", "company".. etc ), mapped to Angular front end as 20 input fields. Now the requirement asks to validate each fields that prevent user submit the form if any fields contain special characters.
I can work out the regex pattern but just wondering any simple way to do one validator to all fields.
javascript angular angular-validation
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
Consider a data model contains 20 string fields with different name ("name", "address", "company".. etc ), mapped to Angular front end as 20 input fields. Now the requirement asks to validate each fields that prevent user submit the form if any fields contain special characters.
I can work out the regex pattern but just wondering any simple way to do one validator to all fields.
javascript angular angular-validation
Consider a data model contains 20 string fields with different name ("name", "address", "company".. etc ), mapped to Angular front end as 20 input fields. Now the requirement asks to validate each fields that prevent user submit the form if any fields contain special characters.
I can work out the regex pattern but just wondering any simple way to do one validator to all fields.
javascript angular angular-validation
javascript angular angular-validation
edited Nov 12 at 5:08
HDJEMAI
4,170143855
4,170143855
asked Nov 12 at 2:34
Dreamer
3,1611458132
3,1611458132
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
There is a simple solution for this by using for example only one async validator.
For example you can group all the fields that we should verify whenever one field change in a subform
and then apply a validator to them.
Example:
this.mySubForm = this.fb.group({
field1: ['', [Validators.required]],
field2: ['', [Validators.required]],
...
field20: ['', [Validators.required]]
}, this.validatorAllFields.bind(this)
});
And define the async validator this way:
validatorAllFields(control: FormGroup): any {
if (control) {
if(control.value.field1 don't contain special char … &&
control.value.field2 don't contain special char … &&
…
control.value.field20 don't contain special char &&)
//validation is ok in this case
return null;
else
//validation fails here...
return {'formInvalid': 'true'}
} else {
return null;
}
}
You can do this without any additional package and it will work.
just implement the: do not have any special char for each control, it should be the same function for example.
You can use formGroupName
for the sub form, or even avoid the use of a sub form if you want.
I would like to learn new things; like this is the case say for example, if I have 100+ fields in the form, don't you think to put 100+ fields in the if clause that becomes too long and what about multiple components like Add and Edit?
– Ajay Ojha
Nov 12 at 7:29
add a comment |
up vote
0
down vote
This can be possible with '@rxweb/reactive-form-validators'. Just you have to create a FormGroup object from 'RxFormBuilder' instead of 'FormBuilder'. RxFormBuilder provides a feature to apply a single validation on all fields and also you can apply a validation individual fields.
Here is code example where I have applied 'required' validation on userName field and 'alphaNumeric' validation on all of the fields:
export class AppComponent implements OnInit {
userInfoFormGroup: FormGroup
constructor(
private formBuilder: RxFormBuilder
) { }
ngOnInit(){
this.userInfoFormGroup = this.formBuilder.group({
userName:['',Validators.required],
firstName:['',],
lastName:['',]
}, new FormBuilderConfiguration({applyAllProps:[RxwebValidators.alphaNumeric({allowWhiteSpace:true})]}));
}
}
Steps I have followed to achieve this:
- npm install @rxweb/reactive-form-validators
- import 'RxReactiveFormsModule' in root module.
- Create the FormGroup through 'RxFormBuilder'.
Why I have suggested this approach with @rxweb/reactive-form-validators?
This can be possible with the standard angular approach, please refer the angular doc, If I go with the standard approach to create validators component-wise based on form fields and set if/else clause of fields 20 or more than 20 in the validator. I see with this approach, the code becomes messy and not readable.
The main reason to follow the approach of rxweb, your code should be more readable with less line of code. Because this is your one case to apply the same validation on 20 fields in a single go, there are fair chances the same scenario can be applied in multiple components with more than 20 fields. if that will be the case then rxweb validators would be more helpful than the standard approach.
Please refer to the working example on stackblitz.
More information about rxweb validators. please go to https://rxweb.io
Please let me know if you have any question.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255310%2fany-simple-way-to-apply-one-validation-rule-to-all-fields-by-angular-6%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
There is a simple solution for this by using for example only one async validator.
For example you can group all the fields that we should verify whenever one field change in a subform
and then apply a validator to them.
Example:
this.mySubForm = this.fb.group({
field1: ['', [Validators.required]],
field2: ['', [Validators.required]],
...
field20: ['', [Validators.required]]
}, this.validatorAllFields.bind(this)
});
And define the async validator this way:
validatorAllFields(control: FormGroup): any {
if (control) {
if(control.value.field1 don't contain special char … &&
control.value.field2 don't contain special char … &&
…
control.value.field20 don't contain special char &&)
//validation is ok in this case
return null;
else
//validation fails here...
return {'formInvalid': 'true'}
} else {
return null;
}
}
You can do this without any additional package and it will work.
just implement the: do not have any special char for each control, it should be the same function for example.
You can use formGroupName
for the sub form, or even avoid the use of a sub form if you want.
I would like to learn new things; like this is the case say for example, if I have 100+ fields in the form, don't you think to put 100+ fields in the if clause that becomes too long and what about multiple components like Add and Edit?
– Ajay Ojha
Nov 12 at 7:29
add a comment |
up vote
0
down vote
There is a simple solution for this by using for example only one async validator.
For example you can group all the fields that we should verify whenever one field change in a subform
and then apply a validator to them.
Example:
this.mySubForm = this.fb.group({
field1: ['', [Validators.required]],
field2: ['', [Validators.required]],
...
field20: ['', [Validators.required]]
}, this.validatorAllFields.bind(this)
});
And define the async validator this way:
validatorAllFields(control: FormGroup): any {
if (control) {
if(control.value.field1 don't contain special char … &&
control.value.field2 don't contain special char … &&
…
control.value.field20 don't contain special char &&)
//validation is ok in this case
return null;
else
//validation fails here...
return {'formInvalid': 'true'}
} else {
return null;
}
}
You can do this without any additional package and it will work.
just implement the: do not have any special char for each control, it should be the same function for example.
You can use formGroupName
for the sub form, or even avoid the use of a sub form if you want.
I would like to learn new things; like this is the case say for example, if I have 100+ fields in the form, don't you think to put 100+ fields in the if clause that becomes too long and what about multiple components like Add and Edit?
– Ajay Ojha
Nov 12 at 7:29
add a comment |
up vote
0
down vote
up vote
0
down vote
There is a simple solution for this by using for example only one async validator.
For example you can group all the fields that we should verify whenever one field change in a subform
and then apply a validator to them.
Example:
this.mySubForm = this.fb.group({
field1: ['', [Validators.required]],
field2: ['', [Validators.required]],
...
field20: ['', [Validators.required]]
}, this.validatorAllFields.bind(this)
});
And define the async validator this way:
validatorAllFields(control: FormGroup): any {
if (control) {
if(control.value.field1 don't contain special char … &&
control.value.field2 don't contain special char … &&
…
control.value.field20 don't contain special char &&)
//validation is ok in this case
return null;
else
//validation fails here...
return {'formInvalid': 'true'}
} else {
return null;
}
}
You can do this without any additional package and it will work.
just implement the: do not have any special char for each control, it should be the same function for example.
You can use formGroupName
for the sub form, or even avoid the use of a sub form if you want.
There is a simple solution for this by using for example only one async validator.
For example you can group all the fields that we should verify whenever one field change in a subform
and then apply a validator to them.
Example:
this.mySubForm = this.fb.group({
field1: ['', [Validators.required]],
field2: ['', [Validators.required]],
...
field20: ['', [Validators.required]]
}, this.validatorAllFields.bind(this)
});
And define the async validator this way:
validatorAllFields(control: FormGroup): any {
if (control) {
if(control.value.field1 don't contain special char … &&
control.value.field2 don't contain special char … &&
…
control.value.field20 don't contain special char &&)
//validation is ok in this case
return null;
else
//validation fails here...
return {'formInvalid': 'true'}
} else {
return null;
}
}
You can do this without any additional package and it will work.
just implement the: do not have any special char for each control, it should be the same function for example.
You can use formGroupName
for the sub form, or even avoid the use of a sub form if you want.
answered Nov 12 at 4:57
HDJEMAI
4,170143855
4,170143855
I would like to learn new things; like this is the case say for example, if I have 100+ fields in the form, don't you think to put 100+ fields in the if clause that becomes too long and what about multiple components like Add and Edit?
– Ajay Ojha
Nov 12 at 7:29
add a comment |
I would like to learn new things; like this is the case say for example, if I have 100+ fields in the form, don't you think to put 100+ fields in the if clause that becomes too long and what about multiple components like Add and Edit?
– Ajay Ojha
Nov 12 at 7:29
I would like to learn new things; like this is the case say for example, if I have 100+ fields in the form, don't you think to put 100+ fields in the if clause that becomes too long and what about multiple components like Add and Edit?
– Ajay Ojha
Nov 12 at 7:29
I would like to learn new things; like this is the case say for example, if I have 100+ fields in the form, don't you think to put 100+ fields in the if clause that becomes too long and what about multiple components like Add and Edit?
– Ajay Ojha
Nov 12 at 7:29
add a comment |
up vote
0
down vote
This can be possible with '@rxweb/reactive-form-validators'. Just you have to create a FormGroup object from 'RxFormBuilder' instead of 'FormBuilder'. RxFormBuilder provides a feature to apply a single validation on all fields and also you can apply a validation individual fields.
Here is code example where I have applied 'required' validation on userName field and 'alphaNumeric' validation on all of the fields:
export class AppComponent implements OnInit {
userInfoFormGroup: FormGroup
constructor(
private formBuilder: RxFormBuilder
) { }
ngOnInit(){
this.userInfoFormGroup = this.formBuilder.group({
userName:['',Validators.required],
firstName:['',],
lastName:['',]
}, new FormBuilderConfiguration({applyAllProps:[RxwebValidators.alphaNumeric({allowWhiteSpace:true})]}));
}
}
Steps I have followed to achieve this:
- npm install @rxweb/reactive-form-validators
- import 'RxReactiveFormsModule' in root module.
- Create the FormGroup through 'RxFormBuilder'.
Why I have suggested this approach with @rxweb/reactive-form-validators?
This can be possible with the standard angular approach, please refer the angular doc, If I go with the standard approach to create validators component-wise based on form fields and set if/else clause of fields 20 or more than 20 in the validator. I see with this approach, the code becomes messy and not readable.
The main reason to follow the approach of rxweb, your code should be more readable with less line of code. Because this is your one case to apply the same validation on 20 fields in a single go, there are fair chances the same scenario can be applied in multiple components with more than 20 fields. if that will be the case then rxweb validators would be more helpful than the standard approach.
Please refer to the working example on stackblitz.
More information about rxweb validators. please go to https://rxweb.io
Please let me know if you have any question.
add a comment |
up vote
0
down vote
This can be possible with '@rxweb/reactive-form-validators'. Just you have to create a FormGroup object from 'RxFormBuilder' instead of 'FormBuilder'. RxFormBuilder provides a feature to apply a single validation on all fields and also you can apply a validation individual fields.
Here is code example where I have applied 'required' validation on userName field and 'alphaNumeric' validation on all of the fields:
export class AppComponent implements OnInit {
userInfoFormGroup: FormGroup
constructor(
private formBuilder: RxFormBuilder
) { }
ngOnInit(){
this.userInfoFormGroup = this.formBuilder.group({
userName:['',Validators.required],
firstName:['',],
lastName:['',]
}, new FormBuilderConfiguration({applyAllProps:[RxwebValidators.alphaNumeric({allowWhiteSpace:true})]}));
}
}
Steps I have followed to achieve this:
- npm install @rxweb/reactive-form-validators
- import 'RxReactiveFormsModule' in root module.
- Create the FormGroup through 'RxFormBuilder'.
Why I have suggested this approach with @rxweb/reactive-form-validators?
This can be possible with the standard angular approach, please refer the angular doc, If I go with the standard approach to create validators component-wise based on form fields and set if/else clause of fields 20 or more than 20 in the validator. I see with this approach, the code becomes messy and not readable.
The main reason to follow the approach of rxweb, your code should be more readable with less line of code. Because this is your one case to apply the same validation on 20 fields in a single go, there are fair chances the same scenario can be applied in multiple components with more than 20 fields. if that will be the case then rxweb validators would be more helpful than the standard approach.
Please refer to the working example on stackblitz.
More information about rxweb validators. please go to https://rxweb.io
Please let me know if you have any question.
add a comment |
up vote
0
down vote
up vote
0
down vote
This can be possible with '@rxweb/reactive-form-validators'. Just you have to create a FormGroup object from 'RxFormBuilder' instead of 'FormBuilder'. RxFormBuilder provides a feature to apply a single validation on all fields and also you can apply a validation individual fields.
Here is code example where I have applied 'required' validation on userName field and 'alphaNumeric' validation on all of the fields:
export class AppComponent implements OnInit {
userInfoFormGroup: FormGroup
constructor(
private formBuilder: RxFormBuilder
) { }
ngOnInit(){
this.userInfoFormGroup = this.formBuilder.group({
userName:['',Validators.required],
firstName:['',],
lastName:['',]
}, new FormBuilderConfiguration({applyAllProps:[RxwebValidators.alphaNumeric({allowWhiteSpace:true})]}));
}
}
Steps I have followed to achieve this:
- npm install @rxweb/reactive-form-validators
- import 'RxReactiveFormsModule' in root module.
- Create the FormGroup through 'RxFormBuilder'.
Why I have suggested this approach with @rxweb/reactive-form-validators?
This can be possible with the standard angular approach, please refer the angular doc, If I go with the standard approach to create validators component-wise based on form fields and set if/else clause of fields 20 or more than 20 in the validator. I see with this approach, the code becomes messy and not readable.
The main reason to follow the approach of rxweb, your code should be more readable with less line of code. Because this is your one case to apply the same validation on 20 fields in a single go, there are fair chances the same scenario can be applied in multiple components with more than 20 fields. if that will be the case then rxweb validators would be more helpful than the standard approach.
Please refer to the working example on stackblitz.
More information about rxweb validators. please go to https://rxweb.io
Please let me know if you have any question.
This can be possible with '@rxweb/reactive-form-validators'. Just you have to create a FormGroup object from 'RxFormBuilder' instead of 'FormBuilder'. RxFormBuilder provides a feature to apply a single validation on all fields and also you can apply a validation individual fields.
Here is code example where I have applied 'required' validation on userName field and 'alphaNumeric' validation on all of the fields:
export class AppComponent implements OnInit {
userInfoFormGroup: FormGroup
constructor(
private formBuilder: RxFormBuilder
) { }
ngOnInit(){
this.userInfoFormGroup = this.formBuilder.group({
userName:['',Validators.required],
firstName:['',],
lastName:['',]
}, new FormBuilderConfiguration({applyAllProps:[RxwebValidators.alphaNumeric({allowWhiteSpace:true})]}));
}
}
Steps I have followed to achieve this:
- npm install @rxweb/reactive-form-validators
- import 'RxReactiveFormsModule' in root module.
- Create the FormGroup through 'RxFormBuilder'.
Why I have suggested this approach with @rxweb/reactive-form-validators?
This can be possible with the standard angular approach, please refer the angular doc, If I go with the standard approach to create validators component-wise based on form fields and set if/else clause of fields 20 or more than 20 in the validator. I see with this approach, the code becomes messy and not readable.
The main reason to follow the approach of rxweb, your code should be more readable with less line of code. Because this is your one case to apply the same validation on 20 fields in a single go, there are fair chances the same scenario can be applied in multiple components with more than 20 fields. if that will be the case then rxweb validators would be more helpful than the standard approach.
Please refer to the working example on stackblitz.
More information about rxweb validators. please go to https://rxweb.io
Please let me know if you have any question.
edited Nov 12 at 6:08
answered Nov 12 at 4:39
Ajay Ojha
95027
95027
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255310%2fany-simple-way-to-apply-one-validation-rule-to-all-fields-by-angular-6%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown