Form (control | emitValue: true | false ): disable control should show in form.value
See original GitHub issueπ feature request
Relevant Package
This feature request is for @angular/formsDescription
I would like see disable control value in onSubmit when we do (this.myForm.value). There are some situation where we want to disable control with default value. Right now i am preserving that default value in another variable.
Describe the solution youβd like
It is good to have one more optional param to just disable control but you will see value in this.myForm.value.
this.myForm.get('name').disable({onlySelf: true, emitEvent: false, emitValue: true});
third optional param emitValue?: true | false
Describe alternatives youβve considered
Above one
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:9 (4 by maintainers)
Top Results From Across the Web
How to disable form control but keep value - Stack Overflow
Assuming you have a disabled property in your component. You have to retun null not false to enable the input. Share.
Read more >Disabling Form Controls When Working With Reactive Forms ...
Instantiate a new FormControl with the disabled property set to true. FormControl({value: '', disabled: true}) . Calling control.disable() or control.enable() .
Read more >FormControl - Angular
Default is false. emitEvent : When true or not supplied (the default), both the statusChanges and valueChanges observables emit events with the latest...
Read more >A thorough exploration of Angular Forms - InDepth.Dev
valueAccessor) _throwError(dir, 'No value accessor for form control with'); /* . ... markAsDirty(); // `{emitModelToViewChange: false}` will make sure thatΒ ...
Read more >Angular β Disable a Form while Preserving Form Control State
The disabled attribute doesn't work on a form tag as this is not in the html specifications. <form disabled> <input type=βtextβ name=βfirstNameβΒ ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
So whenever we work with form regardless of state we should use getRawValue instead of myForm.value as it serves the same purpose. I have worked with number of forms in my admin panel (even on other projects) i never had any situation where i want to exclude disabled controls.
Btw It would be great if angular team can implement optional third param as i mentioned above with that we do not have to take care anything one method for every situation.
Basically, this issue is about the default behavior of a form when submitted: disabled controls are excluded. If you need to get disabled controls, you can use
getRawValue
. Without lots of community interest, it doesnβt seem worth the API complexity or bundle size cost to add this feature.Note also: we are considering adding a readonly state to form controls which would be submitted; this might be a workaround if implemented.