Formbuilder - Change / Remove validation for field based on selected option in form

Hi,

I’m using the formbuilder with multiple fields where some fields are initially required and some not.

What I struggle is, changing a field to not be required anymore, after some other field in the form has selected a specific value.

For example I got the following form:

this.form = this.formBuilder.group({
      name: ['', Validators.required],
      type: ['', Validators.required],
      year: ['', Validators.required],
      place: ['']
    });

If the user selects value “b” for the field “type”, the field “year” should not have the “Validators.required” option anymore.
Is it possible to do this somehow?

I already searched on google and here, but couldn’t find any good answer.

Search “custom validator” or “custom formcontrol.” You need to build your own Validator and put it inside the formGroup.

Searched for it and tried some examples, but I was able to get rid of the validation once I initiliazed it for either the one type or the other.
Did now a workaround for it with just passing a “fake” value and making it null while adding to the DB.