Default value of an input field not passed after form submission

Hi,
I have a form built with an input field, called device_id, that has a pre-filled value.
It happens that, if the user doesn’t modify the field, in the submit function the values is empty.
If the user writes something, then the right value is passed.

This is the field of the form:

            <ion-input type="text" formControlName="device_id"  value="56" required>
            </ion-input>

This is the form definition:

    this.reportForm = this.formBuilder.group({
        device_id: ['', Validators.compose([])
....

If I submit the form without modifying the content of the field device_id:

  onSubmit(values) {
    console.log('@@ REPORT submit: ', values);

The Chrome console shows:

 {device_id: "", msg_text: "dasdasd",...}

How is it possible?

Thank you

cld

I believe if you put the default value within the code where you are creating the formgroupc you will be good to go.

1 Like
this.reportForm = this.formBuilder.group({
        device_id: ['default_value', [ValidatorsFn]]

check if value change of device_id

this.reportForm.get('device_id').valueChanges.subscribe((val) => console.log(val));

1 Like

Thank you @danieldugger and @nitishrajput01 I didn’t think that I could put the default value in the field declaration.

Now it works.

cld