0

When I try to submit a reactive form, select send null, instead of the value from dropdown. I use Angular 12. I tried also with [ngValue] for the option tag, and it's the same. I attached the html, the typescript component and the module. I used reactive forms for that, I imported that module in the module for that component. I will appreciate any help. Thank you!

TYPESCRIPT COMPONENT

import { Component, OnInit } from '@angular/core';
  import { FormBuilder, FormGroup } from '@angular/forms';
  
  @Component({
  selector: 'app-post-a-job',
  templateUrl: './post-a-job.component.html',
  styleUrls: ['./post-a-job.component.scss']
})
export class PostAJobComponent implements OnInit {
  
  myForm: FormGroup;
 
 jobTypes = [{
    value: 'manager', view: 'Manager'
  }]
 
 constructor(private fb: FormBuilder) { }
 
 ngOnInit(): void {
    this.buildForm();
  }

  buildForm() {
    return this.myForm = this.fb.group({
      jobType: null
    });
  }
 save(form) {
    console.log(JSON.stringify(form.value));
  }
   }
MODULE

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { JobsRoutingModule } from './jobs-routing.module';
import { JobsComponent } from './jobs.component';
import { HttpClientModule } from '@angular/common/http';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { PostAJobComponent } from './components/post-a-job/post-a-job.component';


@NgModule({
  declarations: [
    JobsComponent,
    PostAJobComponent
  ],
  imports: [
    CommonModule,
    JobsRoutingModule,
    FormsModule,
    ReactiveFormsModule,
    HttpClientModule
  ]
})
export class JobsModule { }
HTML

<form [formGroup]="myForm">
 <select formControlName="jobType">
            <option [value]="null">(new customer)</option>
            <option [value]="job.value" *ngFor="let job of jobTypes">
                {{job.view}}
            </option>
</select>
 <button type="submit" (click)="save(myForm)" class="btn btn-default">Submit</button>
</form>

3
  • What's your problem? I don't see anything wrong in your code.. Commented Nov 19, 2021 at 7:48
  • When select that option in the select and I press submit, I console.log the myForm.value in the save function and I have this {"jobType":null} Commented Nov 19, 2021 at 7:52
  • I selected the second option, manager Commented Nov 19, 2021 at 7:53

3 Answers 3

1

Just copied your code into the project, there is no problem, I wonder what your problem is

I found two verification problems

  myForm!: FormGroup;

  save(form:any) {
    console.log(JSON.stringify(form.value));
  }
```
Sign up to request clarification or add additional context in comments.

Comments

1

Even though I dont see anything wrong from your code, but you can try prevent the error by disable the placeholder, like :

<option [value]="null" disabled>(new customer)</option>

And validate your submit process :

save(form) {
  let val = form.value
  if(!val.jobType)return
}

Comments

0

The problem was because it was installed a package jquery-nice-select on my project. Thank you all!

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.