use of org.broadleafcommerce.openadmin.web.form.entity.EntityForm in project BroadleafCommerce by BroadleafCommerce.
the class ErrorsProcessor method populateModelVariables.
@Override
public Map<String, Object> populateModelVariables(String tagName, Map<String, String> tagAttributes, String attributeName, String attributeValue, BroadleafTemplateContext context) {
BindStatus bindStatus = context.getBindStatus(attributeValue);
Map<String, Object> newLocalVars = new HashMap<>();
if (bindStatus.isError()) {
EntityForm form = (EntityForm) ((BindingResult) bindStatus.getErrors()).getTarget();
// Map of tab name -> (Map field Name -> list of error messages)
Map<String, Map<String, List<String>>> result = new HashMap<>();
if (!hideTopLevelFieldErrors) {
for (FieldError err : bindStatus.getErrors().getFieldErrors()) {
// attempt to look up which tab the field error is on. If it can't be found, just use
// the default tab for the group
String tabName = EntityForm.DEFAULT_TAB_NAME;
Tab tab = form.findTabForField(err.getField());
if (tab != null) {
tabName = tab.getTitle();
}
Map<String, List<String>> tabErrors = result.get(tabName);
if (tabErrors == null) {
tabErrors = new HashMap<>();
result.put(tabName, tabErrors);
}
if (err.getField().contains(DynamicEntityFormInfo.FIELD_SEPARATOR)) {
// at this point the field name actually occurs within some array syntax
String fieldName = extractFieldName(err);
String[] fieldInfo = fieldName.split("\\" + DynamicEntityFormInfo.FIELD_SEPARATOR);
Field formField = form.getDynamicForm(fieldInfo[0]).findField(fieldName);
if (formField != null) {
addFieldError(formField.getFriendlyName(), err.getCode(), tabErrors);
} else {
LOG.warn("Could not find field " + fieldName + " within the dynamic form " + fieldInfo[0]);
addFieldError(fieldName, err.getCode(), tabErrors);
}
} else {
if (form.getTabs().size() > 0) {
Field formField = form.findField(err.getField());
if (formField != null) {
addFieldError(formField.getFriendlyName(), err.getCode(), tabErrors);
} else {
LOG.warn("Could not find field " + err.getField() + " within the main form");
addFieldError(err.getField(), err.getCode(), tabErrors);
}
} else {
// this is the code that is executed when a Translations add action contains errors
// this branch of the code just puts a placeholder "tabErrors", to avoid errprProcessor parsing errors, and
// avoids checking on tabs, fieldGroups or fields (which for translations are empty), thus skipping any warning
newLocalVars.put("tabErrors", tabErrors);
return newLocalVars;
}
}
}
}
String translatedGeneralTab = GENERAL_ERRORS_TAB_KEY;
BroadleafRequestContext blcContext = BroadleafRequestContext.getBroadleafRequestContext();
if (blcContext != null && blcContext.getMessageSource() != null) {
translatedGeneralTab = blcContext.getMessageSource().getMessage(translatedGeneralTab, null, translatedGeneralTab, blcContext.getJavaLocale());
}
for (ObjectError err : bindStatus.getErrors().getGlobalErrors()) {
Map<String, List<String>> tabErrors = result.get(GENERAL_ERRORS_TAB_KEY);
if (tabErrors == null) {
tabErrors = new HashMap<>();
result.put(translatedGeneralTab, tabErrors);
}
addFieldError(GENERAL_ERROR_FIELD_KEY, err.getCode(), tabErrors);
}
newLocalVars.put("tabErrors", result);
}
return newLocalVars;
}
use of org.broadleafcommerce.openadmin.web.form.entity.EntityForm in project BroadleafCommerce by BroadleafCommerce.
the class AdminPageController method saveEntity.
@Override
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String saveEntity(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "id") String id, @ModelAttribute(value = "entityForm") EntityForm entityForm, BindingResult result, RedirectAttributes ra) throws Exception {
// Attach the dynamic form info so that the update service will know how to split up the fields
DynamicEntityFormInfo info = new DynamicEntityFormInfo().withCeilingClassName(PageTemplate.class.getName()).withSecurityCeilingClassName(Page.class.getName()).withCriteriaName("constructForm").withPropertyName("pageTemplate");
entityForm.putDynamicFormInfo("pageTemplate", info);
String returnPath = super.saveEntity(request, response, model, pathVars, id, entityForm, result, ra);
if (result.hasErrors()) {
info = entityForm.getDynamicFormInfo("pageTemplate");
if (entityForm.getFields().containsKey("pageTemplate")) {
info.setPropertyValue(entityForm.findField("pageTemplate").getValue());
}
// grab back the dynamic form that was actually put in
EntityForm inputDynamicForm = entityForm.getDynamicForm("pageTemplate");
if (inputDynamicForm != null) {
List<Field> fieldsToChange = new ArrayList<Field>();
String prefix = "pageTemplate" + DynamicEntityFormInfo.FIELD_SEPARATOR;
for (Entry<String, Field> entry : inputDynamicForm.getFields().entrySet()) {
if (entry.getKey().startsWith(prefix)) {
fieldsToChange.add(entry.getValue());
}
}
for (Field f : fieldsToChange) {
inputDynamicForm.getFields().remove(f.getName());
f.setName(f.getName().substring(prefix.length()));
inputDynamicForm.getFields().put(f.getName(), f);
}
}
EntityForm dynamicForm;
if (info.getPropertyValue() != null) {
dynamicForm = getDynamicFieldTemplateForm(info, id, inputDynamicForm);
} else {
dynamicForm = getEntityForm(info, inputDynamicForm);
}
entityForm.putDynamicForm("pageTemplate", dynamicForm);
entityForm.removeListGrid("additionalAttributes");
}
return returnPath;
}
use of org.broadleafcommerce.openadmin.web.form.entity.EntityForm in project BroadleafCommerce by BroadleafCommerce.
the class AdminOfferController method addDuplicateOption.
protected void addDuplicateOption(Model model, String id) {
if (duplicator.validate(OfferImpl.class, Long.parseLong(id))) {
EntityForm form = (EntityForm) model.asMap().get("entityForm");
EntityFormAction duplicate = new EntityFormAction("duplicate").withButtonClass("duplicate-button").withDisplayText("Duplicate");
form.addAction(duplicate);
}
}
use of org.broadleafcommerce.openadmin.web.form.entity.EntityForm in project BroadleafCommerce by BroadleafCommerce.
the class AdminProductController method viewEntityForm.
@Override
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String viewEntityForm(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "id") String id) throws Exception {
String view = super.viewEntityForm(request, response, model, pathVars, id);
// Skus have a specific toolbar action to generate Skus based on permutations
EntityForm form = (EntityForm) model.asMap().get("entityForm");
ListGridAction generateSkusAction = new ListGridAction(ListGridAction.GEN_SKUS).withDisplayText("Generate_Skus").withIconClass("icon-fighter-jet").withButtonClass("generate-skus").withUrlPostfix("/generate-skus").withActionUrlOverride("/product/" + id + "/additionalSkus/generate-skus");
ListGrid skusGrid = form.findListGrid("additionalSkus");
if (skusGrid != null) {
skusGrid.setCanFilterAndSort(false);
}
ListGrid productOptionsGrid = form.findListGrid("productOptions");
if (productOptionsGrid != null) {
productOptionsGrid.addToolbarAction(generateSkusAction);
}
// list grids. Remove them from the form.
if (ProductBundle.class.isAssignableFrom(Class.forName(form.getEntityType()))) {
form.removeListGrid("additionalSkus");
form.removeListGrid("productOptions");
form.removeField("canSellWithoutOptions");
}
form.removeListGrid("defaultSku.skuAttributes");
return view;
}
use of org.broadleafcommerce.openadmin.web.form.entity.EntityForm in project BroadleafCommerce by BroadleafCommerce.
the class AdminStructuredContentController method saveEntity.
@Override
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String saveEntity(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "id") String id, @ModelAttribute(value = "entityForm") EntityForm entityForm, BindingResult result, RedirectAttributes ra) throws Exception {
// Attach the dynamic form info so that the update service will know how to split up the fields
DynamicEntityFormInfo info = new DynamicEntityFormInfo().withCeilingClassName(StructuredContentType.class.getName()).withSecurityCeilingClassName(StructuredContent.class.getName()).withCriteriaName("constructForm").withPropertyName("structuredContentType");
entityForm.putDynamicFormInfo("structuredContentType", info);
String returnPath = super.saveEntity(request, response, model, pathVars, id, entityForm, result, ra);
if (result.hasErrors()) {
info = entityForm.getDynamicFormInfo("structuredContentType");
info.setPropertyValue(entityForm.findField("structuredContentType").getValue());
// grab back the dynamic form that was actually put in
EntityForm inputDynamicForm = entityForm.getDynamicForm("structuredContentType");
EntityForm dynamicForm = getDynamicFieldTemplateForm(info, id, inputDynamicForm);
entityForm.putDynamicForm("structuredContentType", dynamicForm);
}
return returnPath;
}
Aggregations