-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(forms): add support for disabled controls #10994
Conversation
3b12acb
to
10c46f3
Compare
I don't think you need to use |
Am I right to assume that because 'reset' does not change controls' statuses we won't have any issues regarding atomicity (e.g., the number of emitted events)? |
So controls can only be disabled by Angular. They cannot disable themselves. Is it correct? |
|
107da5a
to
881e3ad
Compare
@@ -175,6 +185,39 @@ export abstract class AbstractControl { | |||
} | |||
} | |||
|
|||
disable({onlySelf, emitEvent}: {onlySelf?: boolean, emitEvent?: boolean} = {}): void { | |||
emitEvent = isPresent(emitEvent) ? emitEvent : true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since we do not compile to Dart anymore, you should be able to use the default value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Edit: we realized that it needs to be this way to ensure something like {onlySelf: true} doesn't set emitEvent to false.
How we should set a conditional disabled form control using Reactive Forms to simulate |
Wondering the same. My natural feeling was to do something like that: |
@marcalj @Krisa You need to pass in the full boxed value, with both new FormControl({value: '', disabled: true}) Looking only for the |
Thanks @kara for the super quick answer. My question was at least how to bind a dynamic While this is eventually something else, your specific example seems to fail, I've reported just before this issue: |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
In HTML5 forms, controls marked as disabled are not considered when calculating the validity or serialized value of the parent form. However in Angular's current forms API, disabled controls still behave like normal controls and are included in all value/validity checks.
This is confusing because it differs from the HTML5 spec, but it's also confusing because it creates situations where a disabled control can make the whole form invalid, and worse, it's not editable in the UI so this situation cannot be remedied without re-enabling the control.
Changes:
We are adding a new status to controls called "DISABLED". A control can either be:
VALID: control has passed all validation checks
INVALID: control has failed a validation check
PENDING: control is in the midst of conducting a validation check
DISABLED: control is exempt from validation checks
These statuses are mutually exclusive. We are also adding disabled and enabled getters to
AbstractControls
.When you disable a control, it's not included in parent validation or value serialization. So if your disabled control is invalid, the parent form can still be valid if it has other valid controls. Group values will also omit the values of disabled controls. Group status is always reduced from the statuses of its children, so if all a group's children are disabled, the group is disabled.
Reactive forms
Using reactive forms, form controls are disabled in the component class. When you disable a control, the
disabled
attribute is added for you in the DOM (so no need to do this yourself).You have a few choices for how to disable a control. If you want the control to be disabled from the start, you can pass a boxed value as the first arg when you instantiate your
FormControl
. This boxed value contains all the form state that cannot be calculated (as validation state, dirtiness, and touched are all derived).Note that if you don't care to set disabled state, you can just pass in a value without the wrapper object like before.
You can also imperatively enable or disable controls with the
enable()
anddisable()
methods:Angular 1 style (template-driven) forms
To disable a control in Angular-1 style forms, just add the disabled attribute or bind to the disabled property. The control will be disabled for you by
ngModel
under the hood.Fixes #4460.
Breaking change: this PR also removes the deprecated optionals API, which has significant overlap with the new API. For more information about that and the changes in this PR, there's more info in the proposal doc.