title: Angular 4 validator to check 2 controls at the same time
body: <p>I have a reactive form with 2 controls (port_start and port_end) that have the following requirements:</p>
  
  <ul>
  <li>Both must have a value</li>
  <li>Their values must be between 0 and 65535</li>
  <li>port_start value must be less than port_end value</li>
  </ul>
  
  <p>This is what I tried so far:</p>
  
  <pre><code>[...]
  this.formModel.addControl('port_start', 
    new FormControl(object.port_start ? object.port_start : 0, 
    [Validators.required, Validators.min(0), Validators.max(65535), this.minMaxValidator('port_start', 'port_end').bind(this)]));
  
  this.formModel.addControl('port_end', 
    new FormControl(object.ort_end ? object.port_end : 0, 
    [Validators.required, Validators.min(0), Validators.max(65535), this.minMaxValidator('port_start', 'port_end').bind(this)]));
  [...]
  </code></pre>
  
  <p>This is the custom validator function:</p>
  
  <pre><code>minMaxValidator = function(startControl : string, endControl : string): ValidatorFn {
    return (control: FormControl): {[key: string]: any} =&gt; {
      let valid = true;
      let valStart = 0;
      let valEnd = 0;
  
      if(this.formModel.controls[startControl] &amp;&amp; this.formModel.controls[endControl]) {
        valStart = &lt;number&gt;this.formModel.controls[startControl].value;
  
        valEnd = &lt;number&gt;this.formModel.controls[endControl].value;
      }
  
      valid = valEnd &gt;= valStart;
  
      return valid ? null : { minmax : true };
    };
  }
  </code></pre>
  
  <p>This works fine except for this problem:</p>
  
  <ul>
  <li>Let's say I type '2' in the 'port_start' field. Angular marks it as non valid because it's more than the value of 'port_end' (which is 0 by default). If I type '5' in the 'port_end' field, the app still shows 'port_start' as invalid, although now it's correct. </li>
  </ul>
  
  <p>I understand that the problem is that I need to re-check the associated field each time I change the other one's value, but I don't know how to do it.</p>
  
  <p>Any ideas? Thanks,</p>