Skip to main content
added 156 characters in body
Source Link

I am using FormBuilder to build a form :

this.myForm = this.fb.group({
  'name': new FormControl(null, Validators.min(5)),
  'description': new FormControl(null, Validators.min(60))
});

The problem is I should also validate if it is required and I get it from configuration through a map built like this:

map = new Map<string, boolean>();
map.set('name', true);
map.set('description', false);

The problem can be solved as follows:

this.myForm = this.fb.group({
  'name': this.map.get('name') ? new FormControl(null, Validators.compose([Validators.required, Validators.min(5)])) : new FormControl(null, Validators.min(5)),
  'description': this.map.get('description') ? new FormControl(null, Validators.compose([Validators.required, Validators.min(60)])) : new FormControl(null, Validators.min(60))
});

That works but imagine in a large application with several forms with lots of fields really is uncomfortable this way of doing it. Ideally it would look something like this:

Object.keys(this.myForm.controls).forEach(key => {
  if (map.get(key)) {
    this.myForm.get(key).setValidators(this.myForm.get(key).getValidators().push(Validators.required))
  }
});

Of course that way does not work. So if someone who has already solved this or has a good way to do what is described above I will thank you very much. Thanks in advance!!!

Update: The problem can be reduced to "how to get all validators associated with a FormControl". I need help please.do not think there is a definitive solution to this question --> https://github.com/angular/angular/issues/13461

I am using FormBuilder to build a form :

this.myForm = this.fb.group({
  'name': new FormControl(null, Validators.min(5)),
  'description': new FormControl(null, Validators.min(60))
});

The problem is I should also validate if it is required and I get it from configuration through a map built like this:

map = new Map<string, boolean>();
map.set('name', true);
map.set('description', false);

The problem can be solved as follows:

this.myForm = this.fb.group({
  'name': this.map.get('name') ? new FormControl(null, Validators.compose([Validators.required, Validators.min(5)])) : new FormControl(null, Validators.min(5)),
  'description': this.map.get('description') ? new FormControl(null, Validators.compose([Validators.required, Validators.min(60)])) : new FormControl(null, Validators.min(60))
});

That works but imagine in a large application with several forms with lots of fields really is uncomfortable this way of doing it. Ideally it would look something like this:

Object.keys(this.myForm.controls).forEach(key => {
  if (map.get(key)) {
    this.myForm.get(key).setValidators(this.myForm.get(key).getValidators().push(Validators.required))
  }
});

Of course that way does not work. So if someone who has already solved this or has a good way to do what is described above I will thank you very much. Thanks in advance!!!

Update: The problem can be reduced to "how to get all validators associated with a FormControl". I need help please.

I am using FormBuilder to build a form :

this.myForm = this.fb.group({
  'name': new FormControl(null, Validators.min(5)),
  'description': new FormControl(null, Validators.min(60))
});

The problem is I should also validate if it is required and I get it from configuration through a map built like this:

map = new Map<string, boolean>();
map.set('name', true);
map.set('description', false);

The problem can be solved as follows:

this.myForm = this.fb.group({
  'name': this.map.get('name') ? new FormControl(null, Validators.compose([Validators.required, Validators.min(5)])) : new FormControl(null, Validators.min(5)),
  'description': this.map.get('description') ? new FormControl(null, Validators.compose([Validators.required, Validators.min(60)])) : new FormControl(null, Validators.min(60))
});

That works but imagine in a large application with several forms with lots of fields really is uncomfortable this way of doing it. Ideally it would look something like this:

Object.keys(this.myForm.controls).forEach(key => {
  if (map.get(key)) {
    this.myForm.get(key).setValidators(this.myForm.get(key).getValidators().push(Validators.required))
  }
});

Of course that way does not work. So if someone who has already solved this or has a good way to do what is described above I will thank you very much. Thanks in advance!!!

Update: The problem can be reduced to "how to get all validators associated with a FormControl". I do not think there is a definitive solution to this question --> https://github.com/angular/angular/issues/13461

added 4 characters in body
Source Link

I am using FormBuilder to build a form :

this.myForm = this.fb.group({
  'name': new FormControl(null, Validators.min(5)),
  'description': new FormControl(null, Validators.min(60))
});

The problem is I should also validate if it is required and I get it from configuration through a map built like this:

map = new Map<string, boolean>();
map.set('name', true);
map.set('description', false);

The problem can be solved as follows:

this.myForm = this.fb.group({
  'name': this.map.get('name') ? new FormControl(null, Validators.compose([Validators.required, Validators.min(5)])) : new FormControl(null, Validators.min(5)),
  'description': this.map.get('description') ? new FormControl(null, Validators.compose([Validators.required, Validators.min(60)])) : new FormControl(null, Validators.min(60))
});

That works but imagine in a large application with several forms with lots of fields really is uncomfortable this way of doing it. Ideally it would look something like this:

Object.keys(this.myForm.controls).forEach(key => {
  if (map.get(key)) {
    this.myForm.get(key).setValidators(this.myForm.get(key).getValidators().push(Validators.required))
  }
});

Of course that way does not work. So if someone who has already solved this or has a good way to do what is described above I will thank you very much. Thanks in advance!!!

Update: The problem can be reduced to "how to get all validators associated with a FormControl". I need help please.

I am using FormBuilder to build a form :

this.myForm = this.fb.group({
  'name': new FormControl(null, Validators.min(5)),
  'description': new FormControl(null, Validators.min(60))
});

The problem is I should also validate if it is required and I get it from configuration through a map built like this:

map = new Map<string, boolean>();
map.set('name', true);
map.set('description', false);

The problem can be solved as follows:

this.myForm = this.fb.group({
  'name': this.map.get('name') ? new FormControl(null, Validators.compose([Validators.required, Validators.min(5)])) : new FormControl(null, Validators.min(5)),
  'description': this.map.get('description') ? new FormControl(null, Validators.compose([Validators.required, Validators.min(60)])) : new FormControl(null, Validators.min(60))
});

That works but imagine in a large application with several forms with lots of fields really is uncomfortable this way of doing it. Ideally it would look something like this:

Object.keys(this.myForm.controls).forEach(key => {
  if (map.get(key)) {
    this.myForm.get(key).setValidators(this.myForm.get(key).getValidators().push(Validators.required))
  }
});

Of course that way does not work. So if someone who has already solved this or has a good way to do what is described above I will thank you very much. Thanks in advance!!!

Update: The problem can be reduced to "how to get validators associated with a FormControl". I need help please.

I am using FormBuilder to build a form :

this.myForm = this.fb.group({
  'name': new FormControl(null, Validators.min(5)),
  'description': new FormControl(null, Validators.min(60))
});

The problem is I should also validate if it is required and I get it from configuration through a map built like this:

map = new Map<string, boolean>();
map.set('name', true);
map.set('description', false);

The problem can be solved as follows:

this.myForm = this.fb.group({
  'name': this.map.get('name') ? new FormControl(null, Validators.compose([Validators.required, Validators.min(5)])) : new FormControl(null, Validators.min(5)),
  'description': this.map.get('description') ? new FormControl(null, Validators.compose([Validators.required, Validators.min(60)])) : new FormControl(null, Validators.min(60))
});

That works but imagine in a large application with several forms with lots of fields really is uncomfortable this way of doing it. Ideally it would look something like this:

Object.keys(this.myForm.controls).forEach(key => {
  if (map.get(key)) {
    this.myForm.get(key).setValidators(this.myForm.get(key).getValidators().push(Validators.required))
  }
});

Of course that way does not work. So if someone who has already solved this or has a good way to do what is described above I will thank you very much. Thanks in advance!!!

Update: The problem can be reduced to "how to get all validators associated with a FormControl". I need help please.

added 121 characters in body
Source Link

I am using FormBuilder to build a form :

this.myForm = this.fb.group({
  'name': new FormControl(null, Validators.min(5)),
  'description': new FormControl(null, Validators.min(60))
});

The problem is I should also validate if it is required and I get it from configuration through a map built like this:

map = new Map<string, boolean>();
map.set('name', true);
map.set('description', false);

The problem can be solved as follows:

this.myForm = this.fb.group({
  'name': this.map.get('name') ? new FormControl(null, Validators.compose([Validators.required, Validators.min(5)])) : new FormControl(null, Validators.min(5)),
  'description': this.map.get('description') ? new FormControl(null, Validators.compose([Validators.required, Validators.min(60)])) : new FormControl(null, Validators.min(60))
});

That works but imagine in a large application with several forms with lots of fields really is uncomfortable this way of doing it. Ideally it would look something like this:

Object.keys(this.myForm.controls).forEach(key => {
  if (map.get(key)) {
    this.myForm.get(key).setValidators(this.myForm.get(key).getValidators().push(Validators.required))
  }
});

Of course that way does not work. So if someone who has already solved this or has a good way to do what is described above I will thank you very much. Thanks in advance!!!

Update: The problem can be reduced to "how to get validators associated with a FormControl". I need help please.

I am using FormBuilder to build a form :

this.myForm = this.fb.group({
  'name': new FormControl(null, Validators.min(5)),
  'description': new FormControl(null, Validators.min(60))
});

The problem is I should also validate if it is required and I get it from configuration through a map built like this:

map = new Map<string, boolean>();
map.set('name', true);
map.set('description', false);

The problem can be solved as follows:

this.myForm = this.fb.group({
  'name': this.map.get('name') ? new FormControl(null, Validators.compose([Validators.required, Validators.min(5)])) : new FormControl(null, Validators.min(5)),
  'description': this.map.get('description') ? new FormControl(null, Validators.compose([Validators.required, Validators.min(60)])) : new FormControl(null, Validators.min(60))
});

That works but imagine in a large application with several forms with lots of fields really is uncomfortable this way of doing it. Ideally it would look something like this:

Object.keys(this.myForm.controls).forEach(key => {
  if (map.get(key)) {
    this.myForm.get(key).setValidators(this.myForm.get(key).getValidators().push(Validators.required))
  }
});

Of course that way does not work. So if someone who has already solved this or has a good way to do what is described above I will thank you very much. Thanks in advance!!!

I am using FormBuilder to build a form :

this.myForm = this.fb.group({
  'name': new FormControl(null, Validators.min(5)),
  'description': new FormControl(null, Validators.min(60))
});

The problem is I should also validate if it is required and I get it from configuration through a map built like this:

map = new Map<string, boolean>();
map.set('name', true);
map.set('description', false);

The problem can be solved as follows:

this.myForm = this.fb.group({
  'name': this.map.get('name') ? new FormControl(null, Validators.compose([Validators.required, Validators.min(5)])) : new FormControl(null, Validators.min(5)),
  'description': this.map.get('description') ? new FormControl(null, Validators.compose([Validators.required, Validators.min(60)])) : new FormControl(null, Validators.min(60))
});

That works but imagine in a large application with several forms with lots of fields really is uncomfortable this way of doing it. Ideally it would look something like this:

Object.keys(this.myForm.controls).forEach(key => {
  if (map.get(key)) {
    this.myForm.get(key).setValidators(this.myForm.get(key).getValidators().push(Validators.required))
  }
});

Of course that way does not work. So if someone who has already solved this or has a good way to do what is described above I will thank you very much. Thanks in advance!!!

Update: The problem can be reduced to "how to get validators associated with a FormControl". I need help please.

edited tags
Link
Claies
  • 22.4k
  • 4
  • 56
  • 81
Loading
Source Link
Loading