This package is an Otter Framework Module.
This module provides utilities to enhance Angular form (asynchronous decorator, additional validator, error store...).
ng add @o3r/forms
Warning
This module requires @o3r/core to be installed.
A Container/presenter architecture was put in place to ensure best reusability/sharing
- form created in presenter - it's the presenter that decides how the data is displayed
- container only values and errors are propagated from the presenter
- container can set the default value
- presenter implements ControlValueAccessor and Validator (or AsyncValidator) interfaces
- propagate the value, the form status and the errors
- container applies FormControlDirective on the presenter html tag
- container sets the default value using formControl directive
- listen to the value and status changes using the same directive
See FORM_STRUCTURE
- interfaces for the error messages provided in @o3r/forms
- the error messages returned by validators are used in the inline error display
- simple/basic/primitive validators - set as a configuration of the presenter
- localization of the associated error messages from the presenter
- the error object associated is computed here and has to be compliant with the store object model
- getFlatControlErrors function is available in @o3r/forms to help with the creation of the associated error object
- custom validators created at container level
- localization of the associated error messages from the container
- custom validators are passed as an input to the presenter
- the error returned by the validator has to be compliant with the form error store model
- a dedicated FormErrorStore is available on @o3r/forms
- allows the display of errors anywhere on the page
- the error object model contains the translation key and params See FORM_VALIDATION and FORM_ERRORS
- The submit is triggered by the submit button in the presenter and an event is emitted
- container captures the event and executes the submit form logic
The answer is that we should avoid as much as possible having multiple form tags in the same page as it adds a lot of complexity. We should try to have only one form tag that encapsulates everything and one submit action.
If multiple forms are really needed, then we found the following solution:
- the submit button is hidden on the presenters
- the submit is triggered from the page
- an observable to trigger the submit is passed as input to the containers;
- AsyncInput decorator is provided in @o3r/forms to be applied on the observable input to ensure performance
- the submit form logic is executed on the containers
- containers emit events when the submit is done
- the page (parent) captures the events and continues its logic
This can be applied also, with only one form on the page, when you don't want a submit button in the presenter.
At the first display of the form, the inline errors (if the form is invalid) are not displayed because the form element is not touched and dirty In case you want to show the inline errors after the submit, you have to:
- register a function in the container to mark touched and dirty the form
- pass the function via an @Output from the presenter and call it before executing the submit logic
- use markAllControlsDirtyAndTouched helper is available in @o3r/forms to mark interactions on given form
See FORM_SUBMIT&INTERCOMMUNICATION
Find more information in the documentation.