title: Performing cross-field validation on Angular 2 Reactive Forms
body: <p>I'm trying to build out a registration form in Angular 2 using the Reactive Forms module.  As such, I have a FormGroup defined for the form, and I can then list validators for each FormControl therein.</p>
  
  <p>Consider this partial class:</p>
  
  <pre><code>export class TestFormComponent implements OnInit {
    form: FormGroup;
    password = new FormControl("", [Validators.required]);
    passwordConfirm = new FormControl("", [Validators.required, this.validatePasswordConfirmation]);
  
    constructor(private fb: FormBuilder) {
    }
  
    ngOnInit() {
      this.form = this.fb.group({
        "password": this.password,
        "passwordConfirm": this.passwordConfirm
      });
    }
  
    validatePasswordConfirmation(fc: FormControl) {
      var pw2 = fc.value;
      var pw = // how do I get this value properly????
  
      if (pw === '') {
        return {err:"Password is blank"};
      }
  
      if (pw2 === '') {
        return {err:"Confirmation password is blank"};
      }
  
      if (pw !== pw2) {
        return {err:"Passwords do not match"}
      }
  
      return null;
    }
  }
  </code></pre>
  
  <p>You can see I have a validator created for the <code>passwordConfirm</code> field, but I don't know how to get the value of the main <code>password</code> field (for use as <code>pw</code> in the validator) to do the comparison.</p>
  
  <p>I can't just reference <code>this.form.value.password</code> because <code>this</code> in the validator doesn't refer to the main class that contains the form.</p>
  
  <p>Any ideas?</p>