0

I'm having trouble with my select element, when I add the formControlName then -- Select Event Type -- doesn't show up anymore (but is still selected in the dropdown). If I remove it, it displays but doesn't validate of course.

This guy has the same issue here, but the answer does not work for me: Adding formControlName makes the default value disappear in my form's dropdown in Angular

<select class="form-control" name="type" id="type" formControlName="type" [appValidatedInputControl]="form.get('type')"> <option [value]="" disabled selected>-- Select Event Type --</option> <option *ngFor="let type of (eventTypes$ | async)" [value]="type">{{ 'eventType.' + type | translate}}</option> </select> 

TS file

export class EventCreationFormComponent implements OnInit { @Input() form: FormGroup; //... 

The parent HTML component where the form is injected:

<app-event-creation-form [form]="form"></app-event-creation-form> 

Parent component TS file

export class EventCreationComponent implements OnInit, OnDestroy { form: FormGroup; //... constructor(private fb: FormBuilder){ this.initForm(); //... } initForm() { this.form = this.eventCreationService.createForm(); const { type, tags } = this.form.controls; this.subs.sink = type.valueChanges.subscribe((typeValue) => { if (typeValue && (tags.untouched || !tags.value || !tags.value.length)) { tags.patchValue([{ value: typeValue, display: typeValue }]); } }); //... } 

The EventCreationService:

@Injectable() export class EventCreationService implements OnDestroy { constructor(private fb: FormBuilder) {} createForm(): FormGroup { return this.attachEventHandlers(this.fb.group( { type: [undefined, [Validators.required]], //... 

Did I miss something?

1
  • In that case set the default value in the form which is source of this formcontrol Commented Apr 11, 2021 at 14:54

4 Answers 4

0

use the [ngValue] directive with any default value (using 0 here):

<select class="form-control" name="type" id="type" formControlName="type" [appValidatedInputControl]="form.get('type')"> <option [ngValue]="0" disabled>-- Select Event Type --</option> <option *ngFor="let type of (eventTypes$ | async)" [ngValue]="type">{{ 'eventType.' + type | translate}}</option> </select> 

and then in your component, (assuming your FormGroup is called customForm):

this.customForm = new FormGroup({ type: new FormControl(0) }) 
Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately it is not working. I have updated my post. I think maybe it it because my form inherits from another parent component. What do you think?
0

Try to remove the square brackets from the value.

If this does not work please create a stackblitz and try to recreate the problem.

<select class="form-control" name="type" id="type" formControlName="type" [appValidatedInputControl]="form.get('type')"> <option value="" disabled selected>-- Select Event Type --</option> <option *ngFor="let type of (eventTypes$ | async)" [value]="type">{{ 'eventType.' + type | translate}}</option> </select> 

Comments

0

Try this

createForm(): FormGroup { return this.attachEventHandlers(this.fb.group( { type: new FormControl('-- Select Event Type --', Validators.required), //... 

Comments

0

NOT use 'selected' to show , -- Select Event Type --, at first

--SelectEventType--

And, when create the form

 this.fb.group( { type: [null, [Validators.required]], ... } 

NOTE: I imagine that your [appValidatedInputControl] is a directive to validate the control. When you use ReactiveForms, you should use Validators not directive.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.