Search in sources :

Example 11 with IBeanValidationResult

use of org.jowidgets.cap.common.api.validation.IBeanValidationResult in project jo-client-platform by jo-source.

the class BeanFormControl method validate.

private IBeanValidationResult validate(final IBeanValidationResult parentResult) {
    final String propertyName = parentResult.getPropertyName();
    final IValidationResult parentValidationResult = parentResult.getValidationResult();
    boolean validationChanged = false;
    IValidationResult validationResult = ValidationResult.ok();
    final IInputControl<?> control = controls.get(propertyName);
    final String propertyLabel = getLabel(propertyName);
    if (bean != null && control != null && EmptyCompatibleEquivalence.equals(control.getValue(), bean.getValue(propertyName))) {
        validationResult = control.validate();
        // use the parent result if the control result is valid and the parent result is not valid
        if (validationResult.isValid() && !parentValidationResult.isValid()) {
            validationResult = parentValidationResult;
        } else if (validationResult.isOk() && !parentValidationResult.isOk()) {
            validationResult = parentValidationResult;
        }
        // change the validation map, if the worst first changed
        final IValidationResult lastResult = validationResults.get(propertyName);
        if (lastResult == null || !validationResult.getWorstFirst().equals(lastResult.getWorstFirst())) {
            boolean hasMessage = false;
            boolean hasHint = false;
            final IAttribute<Object> attribute = attributes.get(propertyName);
            if (!validationResult.isValid() && !control.hasModifications() && !bean.isModified(propertyName) && inputHint != null && attribute != null && attribute.isMandatory() && EmptyCheck.isEmpty(bean.getValue(propertyName))) {
                validationResults.put(propertyName, ValidationResult.infoError(inputHint));
                hasHint = true;
            } else {
                validationResults.put(propertyName, validationResult.withContext(propertyLabel));
                hasMessage = validationResult.getWorstFirst().getType() != MessageType.OK;
            }
            validationChanged = true;
            // update the validation label
            final IValidationResultLabel validationLabel = validationLabels.get(propertyName);
            if (validationLabel != null) {
                if (hasMessage || control.hasModifications() || bean.isModified(propertyName)) {
                    if (!hasHint) {
                        validationLabel.setResult(validationResult);
                    }
                } else {
                    validationLabel.setEmpty();
                }
            }
        }
    } else if (bean != null && control == null) {
        // change the validation map, if the worst first changed
        validationResult = parentValidationResult;
        final IValidationResult lastResult = validationResults.get(propertyName);
        if (lastResult == null || !validationResult.getWorstFirst().equals(lastResult.getWorstFirst())) {
            if (propertyLabel != null) {
                validationResults.put(propertyName, validationResult.withContext(propertyLabel));
            } else {
                validationResults.put(propertyName, validationResult);
            }
            validationChanged = true;
        }
    }
    if (validationChanged) {
        final IValidationResult newValidationResult = validationResult;
        return new IBeanValidationResult() {

            @Override
            public IValidationResult getValidationResult() {
                return newValidationResult.withContext(propertyLabel);
            }

            @Override
            public String getPropertyName() {
                return propertyName;
            }
        };
    } else {
        return null;
    }
}
Also used : IBeanValidationResult(org.jowidgets.cap.common.api.validation.IBeanValidationResult) IValidationResult(org.jowidgets.validation.IValidationResult) IValidationResultLabel(org.jowidgets.api.widgets.IValidationResultLabel)

Example 12 with IBeanValidationResult

use of org.jowidgets.cap.common.api.validation.IBeanValidationResult in project jo-client-platform by jo-source.

the class BeanFormControl method validate.

@Override
public List<IBeanValidationResult> validate(final Collection<IBeanValidationResult> parentResults) {
    final List<IBeanValidationResult> result = new LinkedList<IBeanValidationResult>();
    boolean validationChanged = false;
    for (final IBeanValidationResult parentResult : parentResults) {
        final IBeanValidationResult subResult = validate(parentResult);
        if (subResult != null) {
            result.add(subResult);
            validationChanged = true;
        } else {
            result.add(parentResult);
        }
    }
    if (validationChanged) {
        setValidationCacheDirty();
    }
    return result;
}
Also used : IBeanValidationResult(org.jowidgets.cap.common.api.validation.IBeanValidationResult) LinkedList(java.util.LinkedList)

Aggregations

IBeanValidationResult (org.jowidgets.cap.common.api.validation.IBeanValidationResult)12 LinkedList (java.util.LinkedList)3 BeanValidationException (org.jowidgets.cap.common.api.exception.BeanValidationException)2 IExternalBeanValidator (org.jowidgets.cap.ui.api.bean.IExternalBeanValidator)2 IValidationMessage (org.jowidgets.validation.IValidationMessage)2 IValidationResult (org.jowidgets.validation.IValidationResult)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 IValidationResultLabel (org.jowidgets.api.widgets.IValidationResultLabel)1 BeansValidationException (org.jowidgets.cap.common.api.exception.BeansValidationException)1 KeyType (org.jowidgets.cap.common.api.exception.BeansValidationException.KeyType)1 ServiceCanceledException (org.jowidgets.cap.common.api.exception.ServiceCanceledException)1 IBeanValidationResultListBuilder (org.jowidgets.cap.common.api.validation.IBeanValidationResultListBuilder)1 ValueHolder (org.jowidgets.util.ValueHolder)1 IObserverSet (org.jowidgets.util.collection.IObserverSet)1