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?
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.
var sum = (number1 + number2); $('#sum'). val(sum);
They don't get submitted, because that's what it says in the W3C specification. Controls that are disabled cannot be successful.
If a field is disabled , the value of the field is not sent to the server when the form is submitted.
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())
}
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With