use of org.jowidgets.api.widgets.IValidationResultLabel 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;
}
}
Aggregations