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);
}
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);
}
}
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);
}
}
Aggregations