Expand my Community achievements bar.

SOLVED

AEM Core Components Form, Custom Action

Avatar

Level 7

When registering a servlet for a new post action using the AEM Core Components, how does the form container know that the post will select this servlet? The code used below works...

@component(service = {Servlet.class}, property = {
        ServletResolverConstants.SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_POST,
        ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES + "=" + "test/components/form/actions/customaction",
        ServletResolverConstants.SLING_SERVLET_SELECTORS + "=" + "post"}, immediate = true)
public class CustomActionPostServlet extends SlingAllMethodsServlet {

 

It makes no sense, here you can see that I am trying to map the servlet to a SLING_SERVLET_RESOURCE_TYPES, but how does my custom action get called in this case? 

does the AEM Core Components Forms container have some magic going on?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @ AEMWizard,

There is no magic in AEM Core Components Forms container implementation

This is how it works OOTB:

  1. Any POST request is processed by CoreFormHandlingServlet.
  2. In the background CoreFormHandlingServlet is using FormsHandlingServletHelper.
  3. FormsHandlingServletHelper relies on :formstart parameter that is hidden parameter passed automatically as part of POST request from Core Form Container.
  4. :formstart parameter stores information about path to Core Form Container on specific page (path where it has been used and configured)
  5. Base on information from :formstart parameter actionType property is read from repository.
  6. actionType property holds value (path) of action that as been defined by author on Core Form Container component level.
  7. Path to actionType definition is used to proceed form data.

Examples of definition of OOTB actions can be found under:

  • /apps/core/wcm/components/form/actions/rpc
  • /libs/commerce/components/actions

In general proper way to use/extend OOTB Core Form is to create custom action, I have described how to do this under this topic:

Having above knowledge, lets try to answer your questions. In general it looks that your servlet is working, because your are not using OOTB one, and you have not extended Core Form in the way how it was designed. It seem that your servlet is executed directly instead of CoreFormHandlingServlet.

If you wanted to use custom servlet for from handling, then probably using Core Form was not the best choice. Because you are not using CoreFormHandlingServlet, to handle your form request you will not benefit from features Core Form provides, e.g. validation. The other drawback is a risk that other OOTB actions will not work correctly. Using a custom servlet is not a bad approach in general, but in that case it will be better to write custom form component as well. You do not need custom servlet to plugin custom action into Core Form.

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @ AEMWizard,

There is no magic in AEM Core Components Forms container implementation

This is how it works OOTB:

  1. Any POST request is processed by CoreFormHandlingServlet.
  2. In the background CoreFormHandlingServlet is using FormsHandlingServletHelper.
  3. FormsHandlingServletHelper relies on :formstart parameter that is hidden parameter passed automatically as part of POST request from Core Form Container.
  4. :formstart parameter stores information about path to Core Form Container on specific page (path where it has been used and configured)
  5. Base on information from :formstart parameter actionType property is read from repository.
  6. actionType property holds value (path) of action that as been defined by author on Core Form Container component level.
  7. Path to actionType definition is used to proceed form data.

Examples of definition of OOTB actions can be found under:

  • /apps/core/wcm/components/form/actions/rpc
  • /libs/commerce/components/actions

In general proper way to use/extend OOTB Core Form is to create custom action, I have described how to do this under this topic:

Having above knowledge, lets try to answer your questions. In general it looks that your servlet is working, because your are not using OOTB one, and you have not extended Core Form in the way how it was designed. It seem that your servlet is executed directly instead of CoreFormHandlingServlet.

If you wanted to use custom servlet for from handling, then probably using Core Form was not the best choice. Because you are not using CoreFormHandlingServlet, to handle your form request you will not benefit from features Core Form provides, e.g. validation. The other drawback is a risk that other OOTB actions will not work correctly. Using a custom servlet is not a bad approach in general, but in that case it will be better to write custom form component as well. You do not need custom servlet to plugin custom action into Core Form.

Avatar

Level 2

Hi @lukasz-m
1. What kind of validations are performed by CoreFormHandlingServlet?
2. Is there any way custom action invokes the CoreFormHandlingServlet and then it invokes the CustomServlet?
if yes, can you help me to with the detailed steps to achieve it?