1

Is it possible to add ignorable fields to Angular 2's reactive forms?

To simplify things, look at this Form:

this.form = new FormGroup({
    subTasks: new FormArray([]),
});

Each subTask will be solved by the user and the solution is saved inside the FormArray.

Now in order to display the text of the different subTasks, I added a FormControl called "text" to each subTask. But when the user submits the form, I don't want the task text to be sent to the server along with the user input.

I know I could just not use it, or remove it programmatically before submitting, but I was wondering if there is a way to do it clean. Thanks!

2
  • Are You passing form values like : this.form.getRawValue() Commented Dec 1, 2017 at 5:06
  • No, this.form.value Commented Dec 1, 2017 at 23:21

1 Answer 1

2

What you could utilize is to disable the formfield text, which means that the field is not included in the form object, but you can still use it. Sample:

this.myForm = this.fb.group({
  text: [{value:'text value', disabled: true}],
  somethingElse: ['something else']
});

and in template:

<form [formGroup]="myForm">
  <p>{{myForm.get('text').value}}</p>
  <input formControlName="somethingElse">
</form>

If you want to inspect the whole form object, disabled fields included, you can use:

this.myForm.getRawValue();
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect, that's exactly what I was looking for. Thank you.
Great, glad to hear! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.