Search in sources :

Example 1 with ValidationInfo

use of com.day.cq.wcm.foundation.forms.ValidationInfo in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class FormActionRpcServletTest method testDoPostWithError.

@Test
void testDoPostWithError() throws ServletException {
    MockSlingHttpServletRequest request = context.request();
    request.setParameterMap(ImmutableMap.of("text", "hello"));
    request.setAttribute("cq.form.id", "new_form");
    Resource resource = context.currentResource("/content/container");
    ModifiableValueMap modifiableValueMap = resource.adaptTo(ModifiableValueMap.class);
    modifiableValueMap.put("formEndPointUrl", "http://localhost:" + wireMockPort + "/form/nonExistingEndpoint");
    underTest.doPost(context.request(), context.response());
    assertEquals(200, context.response().getStatus());
    ValidationInfo validationInfo = ValidationInfo.getValidationInfo(request);
    assertNotNull(validationInfo);
}
Also used : ValidationInfo(com.day.cq.wcm.foundation.forms.ValidationInfo) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.jupiter.api.Test)

Example 2 with ValidationInfo

use of com.day.cq.wcm.foundation.forms.ValidationInfo in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class FormActionRpcServlet method sendRedirect.

private void sendRedirect(ValueMap valueMap, SlingHttpServletRequest request, SlingHttpServletResponse response, boolean processFormApiSuccess) throws ServletException {
    String redirect = getMappedRedirect(valueMap.get("redirect", String.class), request.getResourceResolver());
    String errorMessage = valueMap.get("errorMessage", String.class);
    FormsHandlingRequest formRequest = new FormsHandlingRequest(request);
    try {
        if (StringUtils.isNotEmpty(redirect) && processFormApiSuccess) {
            response.sendRedirect(redirect);
        } else {
            if (!processFormApiSuccess && StringUtils.isNotEmpty(errorMessage)) {
                ValidationInfo validationInfo = ValidationInfo.createValidationInfo(request);
                validationInfo.addErrorMessage(null, errorMessage);
            }
            final Resource formResource = (Resource) request.getAttribute(ATTR_RESOURCE);
            request.removeAttribute(ATTR_RESOURCE);
            request.removeAttribute(ComponentContext.BYPASS_COMPONENT_HANDLING_ON_INCLUDE_ATTRIBUTE);
            RequestDispatcher requestDispatcher = request.getRequestDispatcher(formResource);
            if (requestDispatcher != null) {
                requestDispatcher.forward(formRequest, response);
            } else {
                throw new IOException("can't get request dispatcher to forward the response");
            }
        }
    } catch (IOException var13) {
        LOG.error("Error redirecting to {}", redirect);
    }
}
Also used : ValidationInfo(com.day.cq.wcm.foundation.forms.ValidationInfo) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) IOException(java.io.IOException) FormsHandlingRequest(com.day.cq.wcm.foundation.forms.FormsHandlingRequest) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 3 with ValidationInfo

use of com.day.cq.wcm.foundation.forms.ValidationInfo in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ContainerImpl method initModel.

@PostConstruct
private void initModel() {
    FormStructureHelper formStructureHelper = formStructureHelperFactory.getFormStructureHelper(resource);
    request.setAttribute(FormsHelper.REQ_ATTR_FORM_STRUCTURE_HELPER, formStructureHelper);
    this.action = linkHandler.getLink(currentPage).map(Link::getURL).orElse(null);
    String formId = FormsHelper.getFormId(request);
    if (StringUtils.isBlank(id)) {
        id = formId;
    }
    request.setAttribute(FormsHelper.REQ_ATTR_FORMID, getId());
    this.name = id;
    this.dropAreaResourceType = "wcm/foundation/components/responsivegrid/new";
    if (redirect != null) {
        String contextPath = request.getContextPath();
        if (StringUtils.isNotBlank(contextPath) && redirect.startsWith("/")) {
            redirect = contextPath + redirect;
        }
    }
    if (!StringUtils.equals(request.getRequestPathInfo().getExtension(), ExporterConstants.SLING_MODEL_EXTENSION)) {
        runActionTypeInit(formStructureHelper);
    }
    final ValidationInfo info = ValidationInfo.getValidationInfo(request);
    if (info != null) {
        this.errorMessages = info.getErrorMessages(null);
    }
}
Also used : FormStructureHelper(com.day.cq.wcm.foundation.forms.FormStructureHelper) ValidationInfo(com.day.cq.wcm.foundation.forms.ValidationInfo) Link(com.adobe.cq.wcm.core.components.commons.link.Link) PostConstruct(javax.annotation.PostConstruct)

Aggregations

ValidationInfo (com.day.cq.wcm.foundation.forms.ValidationInfo)3 Resource (org.apache.sling.api.resource.Resource)2 Link (com.adobe.cq.wcm.core.components.commons.link.Link)1 FormStructureHelper (com.day.cq.wcm.foundation.forms.FormStructureHelper)1 FormsHandlingRequest (com.day.cq.wcm.foundation.forms.FormsHandlingRequest)1 IOException (java.io.IOException)1 PostConstruct (javax.annotation.PostConstruct)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)1 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)1 MockSlingHttpServletRequest (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest)1 Test (org.junit.jupiter.api.Test)1