mandatoryValues.add(field.getName());
}
}
}
DataBinder binder = new DataBinder(component) {
@Override
protected void checkRequiredFields(MutablePropertyValues mpvs) {
String[] requiredFields = getRequiredFields();
if (!ObjectUtils.isEmpty(requiredFields)) {
Map<String, PropertyValue> propertyValues = new HashMap<String, PropertyValue>();
PropertyValue[] pvs = mpvs.getPropertyValues();
for (PropertyValue pv : pvs) {
String canonicalName = PropertyAccessorUtils.canonicalPropertyName(pv.getName());
propertyValues.put(canonicalName, pv);
}
for (String field : requiredFields) {
PropertyValue pv = propertyValues.get(field);
boolean empty = (pv == null || pv.getValue() == null);
// For our purposes, empty Strings or empty String arrays do not count as
// empty. Empty is only "null".
// if (!empty) {
// if (pv.getValue() instanceof String) {
// empty = !StringUtils.hasText((String) pv.getValue());
// }
// else if (pv.getValue() instanceof String[]) {
// String[] values = (String[]) pv.getValue();
// empty = (values.length == 0 || !StringUtils.hasText(values[0]));
// }
// }
if (empty) {
// Use bind error processor to create FieldError.
getBindingErrorProcessor()
.processMissingFieldError(field, getInternalBindingResult());
// Remove property from property values to bind:
// It has already caused a field error with a rejected value.
if (pv != null) {
mpvs.removePropertyValue(pv);
propertyValues.remove(field);
}
}
}
}
}
};
binder.initDirectFieldAccess();
PropertyEditorUtil.registerUimaFITEditors(binder);
binder.setRequiredFields(mandatoryValues.toArray(new String[mandatoryValues.size()]));
binder.bind(values);
if (binder.getBindingResult().hasErrors()) {
StringBuilder sb = new StringBuilder();
sb.append("Errors initializing [" + component.getClass() + "]");
for (ObjectError error : binder.getBindingResult().getAllErrors()) {
if (sb.length() > 0) {
sb.append("\n");
}
sb.append(error.getDefaultMessage());
}