title: Angular exercise with a few task
body: <p>I'm trying to solve an angular exercise with the following tasks:</p>
  <p>/* MODIFY THE CODE TO ACCOMPLISH THE FOLLOWING</p>
  <ol>
  <li>Enable the Band Name field if all the other fields are filled out</li>
  <li>Modify the bandName() validator to only be valid if the following rules are met
  a) The band name must start with the first letter of of the First Name field
  b) The band name must contain the age</li>
  <li>The background of Band Name must be the appropriate color which corresponds to the department</li>
  <li>Modify the template to restrict age input to only a number
  */</li>
  </ol>
  <p>I kinda got stuck on a very early stage, for some reason i don't get there is no communication at all between the logic and the view component since i'm not able to console log any of the form group attributes. this is the first time i'm working with the form builder group and i'm not really familiar with it. Thanks for everyone. this is my code:</p>
  <p>Script:</p>
  <pre class="lang-js prettyprint-override"><code>import { Component, OnInit } from '@angular/core';
  import {
    FormBuilder,
    FormGroup,
    Validators,
    ValidatorFn,
    AbstractControl,
  } from '@angular/forms';
  @Component({
    selector: 'app-quiz',
    templateUrl: './quiz.component.html',
    styleUrls: ['./quiz.component.css']
  })
  export class QuizComponent implements OnInit {
  
    colorAssociation = {
      Technology: 'lightblue',
      Design: 'lightgreen',
      Education: 'purple'
    };
  
    form: FormGroup;
    bandNameForm: FormGroup;
    constructor(private formBuilder: FormBuilder) {}
  
    getBackgroundColor() {
      let color = '';
      if (this.form) {
        let dept = this.form.value.department;
        if (dept) {
          color=this.colorAssociation[dept];
        }
      }
      return color;
    }
  
  
    ngOnInit() {
      this.form = this.formBuilder.group({
        firstName: ['', Validators.required],
        age: ['', Validators.required],
        department: ['', Validators.required],
      });
      this.bandNameForm = this.formBuilder.group({
        bandName: ['', bandName(this.form)],
      });
      this.form.setValidators(areTheFieldsFilledOut(this.bandNameForm,this.form));
      this.bandNameForm.controls.bandName.disable();
    }
  
  
  }
  
  export function bandName(form: FormGroup): ValidatorFn {
    return (control: AbstractControl): { [key: string]: any } | null =&gt; {
      let error = 'Invalid';
      let value = control.value;
      let firstLetter = '';
      let age;
      if (form) {
        let firstName = form.value.firstName;
        age = form.value.age;
        console.log(value);
  
        if (firstName) {
          firstLetter = firstName[0];
        }
        if (value.indexOf(firstLetter)===0 &amp;&amp; value.includes(age)) {
          error=null;
        }
      }
      // fill out the rest of the validator logic
      return error ? { error: { value: error } } : null;
    };
  }
  
  
  export function areTheFieldsFilledOut(bandNameForm: FormGroup, form:FormGroup): ValidatorFn {
    return (control:AbstractControl): { [key: string]: any } | null =&gt; {
      let error = 'Invalid';
      
      if (
        !(form.value.age === '') &amp;&amp;
        !(form.value.department === '') &amp;&amp;
        !(form.value.firstname === '')
      ) {
        bandNameForm.controls.bandName.enable();
      }
      return error ? { error: { value: error } } : null;
    };
  
  
  }
  </code></pre>
  <p>Template:</p>
  <pre class="lang-html prettyprint-override"><code>&lt;form [formGroup]=&quot;form&quot;&gt;
          
      &lt;label for=&quot;firstName&quot;&gt;First Name
          &lt;input type=&quot;text&quot;
          formControlName=&quot;firstName&quot;
          &gt;
      &lt;/label&gt;
      &lt;label for=&quot;age&quot;&gt;age
          &lt;input type=&quot;number&quot;
          formControlName=&quot;age&quot;
          &gt;
      &lt;/label&gt;
      &lt;br&gt;&lt;br&gt;
      &lt;h4&gt;Department:&lt;/h4&gt;
      &lt;br&gt;&lt;br&gt;
      &lt;p&gt;
          &lt;label&gt;
              &lt;input formControlName=&quot;department&quot; value=&quot;technology&quot; type=&quot;radio&quot;&gt;
              &lt;span&gt;Technology&lt;/span&gt;
          &lt;/label&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;label&gt;
              &lt;input formControlName=&quot;department&quot; value=&quot;education&quot; type=&quot;radio&quot; /&gt;
              &lt;span&gt;Education&lt;/span&gt;
          &lt;/label&gt;
      &lt;/p&gt;
       &lt;p&gt;
          &lt;label&gt;
              &lt;input formControlName=&quot;department&quot; value=&quot;design&quot; type=&quot;radio&quot;  /&gt;
              &lt;span&gt;Design&lt;/span&gt;
          &lt;/label&gt;
      &lt;/p&gt;
      &lt;br&gt;&lt;br&gt;
  
      &lt;form [formGroup]=&quot;bandNameForm&quot;&gt;
          &lt;label&gt;
              &lt;input type=&quot;text&quot; formControlName=&quot;band&quot;&gt;
              &lt;span&gt;Band Name&lt;/span&gt;
          &lt;/label&gt;
  
      &lt;/form&gt;
      &lt;br&gt;&lt;br&gt;
          
      &lt;button type=&quot;submit&quot; class=&quot;waves-effect waves-light btn&quot;&gt;Submit&lt;/button&gt;
  
  &lt;/form&gt;
  </code></pre>