Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the value of an input disabled?

I'm creating a reactive form in angular 2 and using material angular 2 to inputs;

I need to set an input as disabled and when submitting the value that is in the unlocked input the value to be sent. I Already created the input and the disabled is running. My problem is that when I give a submit value it does not come.

My code

this.formVariacao = this._fb.group({

   id:[p && p['id']],
   codigoItem: [p && p['codigoItem'], Validators.required],
});

My html

<md-input-container>
    <input mdInput placeholder="Código da Variação" formControlName="codigoItem" name="codigoItem">
</md-input-container>

I block the input as follows

this.formVariacao.get('codigoItem').disable();

Only when I send the form the value does not come. Alguém pode me ajudar?

like image 784
Jobsdev Avatar asked Jun 04 '17 07:06

Jobsdev


People also ask

How do I enable input field Disabled?

Projects In JavaScript & JQuery We can easily disable input box(textbox,textarea) using disable attribute to “disabled”. $('elementname'). attr('disabled','disabled'); To enable disabled element we need to remove “disabled” attribute from this element.

How get textbox value from disabled in PHP?

var sum = (number1 + number2); $('#sum'). val(sum);

Does disabled input get submitted?

They don't get submitted, because that's what it says in the W3C specification. Controls that are disabled cannot be successful.

Are Disabled inputs sent?

If a field is disabled , the value of the field is not sent to the server when the form is submitted.


2 Answers

As you noticed Angular ignores disabled form controls in the form object.

This can be easily fixed by using getRawValue(), which includes all form controls, disabled or not. So for example on your submit, you pass the form (NOT the form value) like so:

(ngSubmit)="onSubmit(formVariacao)" 

Then you can use getRawValue:

onSubmit(form) {
  console.log(form.getRawValue())
}
like image 64
AT82 Avatar answered Oct 21 '22 15:10

AT82


From the code docs :

 /**
     * A control is `disabled` when its `status === DISABLED`.
     *
     * Disabled controls are exempt from validation checks and
     * are not included in the aggregate value of their ancestor
     * controls.
     */

So if you're using disable() only to make the control to have disabled attribute, don't use disable(), use [attr.disbaled]="yourVariable"

LIke this :

<input [attr.disabled]="disabled" mdInput placeholder="Código da Variação" formControlName="codigoItem" name="codigoItem">

and then in your typescript :

instead of

 this.formVariacao.get('codigoItem').disable(); 

Do this :

  private disabled = false;



  this.disabled = true;
like image 45
Milad Avatar answered Oct 21 '22 14:10

Milad