/** * Validate cross-field BeanValidation rules. * * @param origin screen controller * @param item item to validate * @return validation errors */ public ValidationErrors validateCrossFieldRules(@SuppressWarnings("unused") FrameOwner origin, Entity item) { ValidationErrors errors = new ValidationErrors(); Validator validator = beanValidation.getValidator(); Set<ConstraintViolation<Entity>> violations = validator.validate(item, UiCrossFieldChecks.class); violations.stream() .filter(violation -> { Path propertyPath = violation.getPropertyPath(); Path.Node lastNode = Iterables.getLast(propertyPath); return lastNode.getKind() == ElementKind.BEAN; }) .forEach(violation -> errors.add(violation.getMessage())); return errors; }
public void validateAdditionalRules(ValidationErrors errors) { // all previous validations return no errors if (crossFieldValidate && errors.isEmpty()) { BeanValidation beanValidation = getBeanLocator().get(BeanValidation.NAME); Validator validator = beanValidation.getValidator(); Set<ConstraintViolation<Entity>> violations = validator.validate(getItem(), UiCrossFieldChecks.class); violations.stream() .filter(violation -> { Path propertyPath = violation.getPropertyPath(); Path.Node lastNode = Iterables.getLast(propertyPath); return lastNode.getKind() == ElementKind.BEAN; }) .forEach(violation -> errors.add(violation.getMessage())); } }
@Override protected void postValidate(ValidationErrors errors) { super.postValidate(errors); //remove errors from widget frames errors.getAll().removeIf(error -> !"dashboard$dashboardEditFieldGroup1".equals(error.component.getParent().getId())); List<Widget> dashboardWidgets = dashboardDs.getItem().getWidgets(); Map<String, Long> widgetsCount = dashboardWidgets.stream() .collect(Collectors.groupingBy(Widget::getWidgetId, Collectors.counting())); List<String> nonUniqueIds = widgetsCount.entrySet().stream() .filter(es -> es.getValue() > 1) .map(Map.Entry::getKey) .collect(toList()); if (nonUniqueIds.size() > 0) { errors.add(null, formatMessage("uniqueWidgetId", String.join(",", nonUniqueIds))); } }
protected void focusProblemComponent(ValidationErrors errors) { com.haulmont.cuba.gui.components.Component component = null; if (!errors.getAll().isEmpty()) { component = errors.getFirstComponent(); } if (component != null) { ComponentsHelper.focusComponent(component); } }
/** * Add an error without reference to component causing it. * @param description error description */ public void add(String description) { add(null, description); }
protected boolean handleValidationErrors(ValidationErrors errors) { if (errors.isEmpty()) return true; WebComponentsHelper.focusProblemComponent(errors); return false; }
@Override public boolean validate(List<Validatable> fields) { ValidationErrors errors = new ValidationErrors(); for (Validatable field : fields) { try { field.validate(); } catch (ValidationException e) { if (log.isTraceEnabled()) log.trace("Validation failed", e); else if (log.isDebugEnabled()) log.debug("Validation failed: " + e); ComponentsHelper.fillErrorMessages(field, e, errors); } } return handleValidationErrors(errors); }
protected void validateAdditionalRules(ValidationErrors errors) { // all previous validations return no errors if (isCrossFieldValidate() && errors.isEmpty()) { ScreenValidation screenValidation = getBeanLocator().get(ScreenValidation.NAME); ValidationErrors validationErrors = screenValidation.validateCrossFieldRules(this, getEditedEntity()); errors.addAll(validationErrors); } }
/** * Show validation alert with passed errors and first problem UI component. * * @param origin screen controller * @param errors validation error */ public void showValidationErrors(FrameOwner origin, ValidationErrors errors) { checkNotNullArgument(origin); checkNotNullArgument(errors); if (errors.isEmpty()) { return; } StringBuilder buffer = new StringBuilder(); for (ValidationErrors.Item error : errors.getAll()) { buffer.append(error.description).append("\n"); } ClientConfig clientConfig = configuration.getConfig(ClientConfig.class); String validationNotificationType = clientConfig.getValidationNotificationType(); if (validationNotificationType.endsWith("_HTML")) { // HTML validation notification types are not supported validationNotificationType = validationNotificationType.replace("_HTML", ""); } Notifications notifications = getScreenContext(origin).getNotifications(); notifications.create(NotificationType.valueOf(validationNotificationType)) .withCaption(messages.getMainMessage("validationFail.caption")) .withDescription(buffer.toString()) .show(); focusProblemComponent(errors); }
@Deprecated public static void focusProblemComponent(ValidationErrors errors) { com.haulmont.cuba.gui.components.Component problemComponent = errors.getFirstComponent(); if (problemComponent != null) { ComponentsHelper.focusComponent(problemComponent); } }
/** * Show validation errors alert. Can be overridden in subclasses. * * @param errors the list of validation errors. Caller fills it by errors found during the default validation. */ public void showValidationErrors(ValidationErrors errors) { StringBuilder buffer = new StringBuilder(); for (ValidationErrors.Item error : errors.getAll()) { buffer.append(error.description).append("\n"); } Configuration configuration = AppBeans.get(Configuration.NAME); ClientConfig clientConfig = configuration.getConfig(ClientConfig.class); NotificationType notificationType = NotificationType.valueOf(clientConfig.getValidationNotificationType()); showNotification(messages.getMainMessage("validationFail.caption"), buffer.toString(), notificationType); }
protected void checkForNameDuplication(ValidationErrors errors, Multimap<String, BandDefinition> names) { for (String name : names.keySet()) { Collection<BandDefinition> bandDefinitionsWithsSameNames = names.get(name); if (bandDefinitionsWithsSameNames != null && bandDefinitionsWithsSameNames.size() > 1) { errors.add(formatMessage("error.bandNamesDuplicated", name)); } } }
protected boolean handleValidationErrors(ValidationErrors errors) { if (errors.isEmpty()) { return true; } WebComponentsHelper.focusProblemComponent(errors); return false; }
@Override public boolean validate(List<Validatable> fields) { ValidationErrors errors = new ValidationErrors(); for (Validatable field : fields) { try { field.validate(); } catch (ValidationException e) { if (log.isTraceEnabled()) log.trace("Validation failed", e); else if (log.isDebugEnabled()) log.debug("Validation failed: " + e); ComponentsHelper.fillErrorMessages(field, e, errors); } } return handleValidationErrors(errors); }
/** * Validates the cross-field rules if passed validation errors are empty. * * @param errors errors found during components validation */ protected void validateAdditionalRules(ValidationErrors errors) { // all previous validations return no errors if (isCrossFieldValidate() && errors.isEmpty()) { ScreenValidation screenValidation = getBeanLocator().get(ScreenValidation.NAME); ValidationErrors validationErrors = screenValidation.validateCrossFieldRules(this, getEditContainer().getItem()); errors.addAll(validationErrors); } }
@Override protected boolean preCommit() { if (super.preCommit()) { PivotTableAggregation aggregation = getItem(); boolean hasMatches = existingItems.stream(). anyMatch(e -> !Objects.equals(aggregation, e) && Objects.equals(aggregation.getCaption(), e.getCaption())); if (hasMatches) { ValidationErrors validationErrors = new ValidationErrors(); validationErrors.add(getMessage("pivotTableEdit.uniqueAggregationOptionCaption")); showValidationErrors(validationErrors); return false; } return true; } return false; }
@Override protected void postValidate(ValidationErrors errors) { super.postValidate(errors); if (fileUpload.getFileId() == null) { errors.add(getMessage("reportException.noFile")); return; } String extension = FilenameUtils.getExtension(fileUpload.getFileName()); if (!StringUtils.equalsIgnoreCase(extension, ExportFormat.ZIP.getFileExt())) { errors.add(formatMessage("reportException.wrongFileType", extension)); } } }
@Override protected void postValidate(ValidationErrors errors) { super.postValidate(errors); String password = passwField.getValue(); if (errors.isEmpty()) { String currentPassword = currentPasswordField.getValue(); String passwordConfirmation = confirmPasswField.getValue(); UUID targetUserId; if (user == null) { targetUserId = userSession.getUser().getId(); } else { targetUserId = user.getId(); } if (currentPasswordRequired && !userManagementService.checkPassword(targetUserId, currentPassword)) { errors.add(currentPasswordField, getMessage("wrongCurrentPassword")); } else if (userManagementService.checkPassword(targetUserId, password)) { errors.add(passwField, getMessage("currentPasswordWarning")); } else if (!Objects.equals(password, passwordConfirmation)) { errors.add(confirmPasswField, getMessage("passwordsDoNotMatch")); } else { if (clientConfig.getPasswordPolicyEnabled()) { String regExp = clientConfig.getPasswordPolicyRegExp(); if (!password.matches(regExp)) { errors.add(passwField, getMessage("simplePassword")); } } } } }
protected boolean handleValidationErrors(ValidationErrors errors) { postValidate(errors); if (errors.isEmpty()) return true; ScreenValidation screenValidation = getBeanLocator().get(ScreenValidation.class); screenValidation.showValidationErrors(this, errors); return false; }
/** * Validates UI components by invoking their {@link Validatable#validate()}. * * @param container components container * @return validation errors */ public ValidationErrors validateUiComponents(ComponentContainer container) { ValidationErrors errors = new ValidationErrors(); ComponentsHelper.traverseValidatable(container, v -> validate(v, errors) ); return errors; }