typescript - ¿podemos comprobar si un formulario está deshabilitado o no en un formulario reactivo de Angular?
Estoy usando el formulario reactivo en mi Angular9 para la validación del formulario. Ahora necesito ocultar el botón de envío que está fuera de este formulario en porcentaje, si el formulario está deshabilitado (todos los campos del formulario están deshabilitados). ¿Hay alguna variable o método en FormGroup para lograr esto?
formValidationConfig() {
this.userDataUpdateForm = new FormBuilder().group({
language: [''],
firstname: ['', [Validators.required, Validators.minLength(3), Validators.maxLength(20), Validators.pattern('^(?!_)^[a-zA-Z0-9_]*$')]],
lastname: ['', Validators.pattern('^(?!_)^[a-zA-Z0-9_]*$')],
gender: ['', Validators.required],
pan: [''],
mobile: ['', [Validators.minLength(10), Validators.maxLength(10)]],
email: ['', Validators.email],
address: [''],
pincode: [''],
city: [''],
state: [''],
country: [''],
day: ['', Validators.required],
month: ['' , Validators.required],
year: ['', Validators.required],
otp: ['']
});
}
disableFields() {
try {
if (this.userProfileData.pref_lang.length > 0) { this.f.language.disable(); }
if (this.userProfileData.firstname?.length > 0) { this.f.firstname.disable(); }
if (this.userProfileData.lastname?.length > 0) { this.f.lastname.disable(); }
if (this.userProfileData.gender?.length > 0) { this.f.gender.disable(); }
if (this.userProfileData.pan_number.length > 0) { this.f.pan.disable(); }
if (this.userProfileData.language?.length > 0) { this.f.language.disable(); }
if (this.userProfileData?.email_status === 'active') { this.f.email.disable(); }
if (this.userProfileData?.mobile_verification === '1') { this.f.mobile.disable(); }
if (this.userProfileData?.address?.length > 0) { this.f.address.disable(); }
if (this.userProfileData?.pincode?.length > 0) { this.f.pincode.disable(); }
if (this.userProfileData?.city?.length > 0) { this.f.city.disable(); }
if (this.userProfileData?.state_name?.length > 0) { this.f.state.disable(); }
if (this.userProfileData?.country?.length > 0) { this.f.country.disable(); }
if (this.userProfileData.day) { this.f.day.disable(); }
if (this.userProfileData.month ) { this.f.month.disable(); }
if (this.userProfileData.year) { this.f.year.disable(); }
} catch (error) { console.log('Profile disabled error', error); }
console.log('disabled obj--->');
}
Respuesta #1
Sin conocer tu situación exacta, puedes probar algo como esto:
<button *ngIf="!form.disabled">Save</button>
El grupo de formularios tiene una propiedad deshabilitada, que será verdadera si el grupo de formularios está configurado como deshabilitado.
