2

i keep getting a null value from my reactive form. The field name is "from"

my ts file

  ngOnInit() {
this.createForm = new FormGroup({
  'title': new FormControl(null, [
            Validators.required,
            Validators.maxLength(40)
          ]),
  'message': new FormControl(null, [
            Validators.required,
            Validators.maxLength(130)
          ]),
  'from': new FormControl(null,
            Validators.maxLength(40)
          )
});

}

my form

<form [formGroup]="createForm" (ngSubmit)="onSubmit()">
  <div class="form-group">
    <label for="">Food Item</label>
    <input type="text"
          class="form-control"
          placeholder="Bacon Egg & Cheese Sandwich"
          maxlength="40"
          formControlName="title"
      >
  </div>
  <div class="form-group">
    <label for="">Customer Message to Recepient</label>
    <textarea         
        class="form-control"
        rows="4"
        maxlength="130"
        formControlName="message"></textarea>
  </div>
  <div class="form-group">
      <label for="">Customers Name</label><br>
      <input type="text"
      class="form-control"
      placeholder="Anonymous"
      maxlength="40"
      formControlName="from"
      >
      <small class="text-muted">Leave Blank for Anonymous</small>
  </div>
  <button class="btn btn-success">Create</button>
</form>

in my onSubmit() method, this.createForm.controls['from'].value keeps giving me a null

anyone know what i'm doing wrong?

3
  • "message" and "title" fields are both getting values Commented Feb 19, 2018 at 0:53
  • What you have works fine for me: stackblitz.com/edit/angular-okddew?file=src/app/… Commented Feb 19, 2018 at 1:13
  • i found what was wrong. i was expecting an empty string but instead i got null. I was basing my logic on an empty string when i should have based in on null. Commented Feb 19, 2018 at 2:59

1 Answer 1

1

i found what was wrong. i was expecting an empty string but instead i got null. I was basing my logic on an empty string when i should have based in on null.

The reactive form was doing exactly what it was supposed to.

Sign up to request clarification or add additional context in comments.

Comments

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.