use of org.springframework.validation.ObjectError in project spring-boot by spring-projects.
the class DefaultErrorAttributesTests method extractBindingResultErrors.
@Test
public void extractBindingResultErrors() throws Exception {
BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
bindingResult.addError(new ObjectError("c", "d"));
Exception ex = new BindException(bindingResult);
testBindingResult(bindingResult, ex);
}
use of org.springframework.validation.ObjectError in project opennms by OpenNMS.
the class DefaultDistributedPollerServiceTest method testResumeLocationMonitorNotPaused.
public void testResumeLocationMonitorNotPaused() {
OnmsLocationMonitor locationMonitor = new OnmsLocationMonitor();
locationMonitor.setId(LOCATION_MONITOR_ID);
locationMonitor.setStatus(MonitorStatus.STARTED);
expect(m_locationMonitorDao.load(locationMonitor.getId())).andReturn(locationMonitor);
LocationMonitorIdCommand command = new LocationMonitorIdCommand();
command.setMonitorId(LOCATION_MONITOR_ID);
BindException errors = new BindException(command, "command");
replayMocks();
m_distributedPollerService.resumeLocationMonitor(command, errors);
verifyMocks();
assertEquals("error count", 1, errors.getErrorCount());
List<ObjectError> errorList = getErrorList(errors);
assertEquals("error list count", 1, errorList.size());
assertEquals("error 0 code", "distributed.locationMonitor.notPaused", errorList.get(0).getCode());
}
use of org.springframework.validation.ObjectError in project opennms by OpenNMS.
the class DefaultDistributedPollerServiceTest method testPauseLocationMonitorBindingErrors.
public void testPauseLocationMonitorBindingErrors() {
LocationMonitorIdCommand command = new LocationMonitorIdCommand();
BindException errors = new BindException(command, "command");
errors.addError(new ObjectError("foo", null, null, "foo"));
assertEquals("error count before pause", 1, errors.getErrorCount());
replayMocks();
m_distributedPollerService.pauseLocationMonitor(command, errors);
verifyMocks();
assertEquals("error count after pause", 1, errors.getErrorCount());
}
use of org.springframework.validation.ObjectError in project ORCID-Source by ORCID.
the class RegistrationController method regEmailValidate.
public Registration regEmailValidate(HttpServletRequest request, Registration reg, boolean isOauthRequest, boolean isKeyup) {
reg.getEmail().setErrors(new ArrayList<String>());
if (!isKeyup && (reg.getEmail().getValue() == null || reg.getEmail().getValue().trim().isEmpty())) {
setError(reg.getEmail(), "Email.registrationForm.email");
}
String emailAddress = reg.getEmail().getValue();
MapBindingResult mbr = new MapBindingResult(new HashMap<String, String>(), "Email");
// Validate the email address is ok
if (!validateEmailAddress(emailAddress)) {
String[] codes = { "Email.personalInfoForm.email" };
String[] args = { emailAddress };
mbr.addError(new FieldError("email", "email", emailAddress, false, codes, args, "Not vaild"));
} else {
//If email exists
if (emailManager.emailExists(emailAddress)) {
String orcid = emailManager.findOrcidIdByEmail(emailAddress);
String[] args = { emailAddress };
//If it is claimed, should return a duplicated exception
if (profileEntityManager.isProfileClaimedByEmail(emailAddress)) {
String[] codes = null;
if (profileEntityManager.isDeactivated(orcid)) {
codes = new String[] { "orcid.frontend.verify.deactivated_email" };
} else {
codes = new String[] { "orcid.frontend.verify.duplicate_email" };
}
mbr.addError(new FieldError("email", "email", emailAddress, false, codes, args, "Email already exists"));
} else {
if (profileEntityManager.isDeactivated(orcid)) {
String[] codes = new String[] { "orcid.frontend.verify.deactivated_email" };
mbr.addError(new FieldError("email", "email", emailAddress, false, codes, args, "Email already exists"));
} else if (!emailManager.isAutoDeprecateEnableForEmail(emailAddress)) {
//If the email is not eligible for auto deprecate, we should show an email duplicated exception
String resendUrl = createResendClaimUrl(emailAddress, request);
String[] codes = { "orcid.frontend.verify.unclaimed_email" };
args = new String[] { emailAddress, resendUrl };
mbr.addError(new FieldError("email", "email", emailAddress, false, codes, args, "Unclaimed record exists"));
} else {
LOGGER.info("Email " + emailAddress + " belongs to a unclaimed record and can be auto deprecated");
}
}
}
}
for (ObjectError oe : mbr.getAllErrors()) {
Object[] arguments = oe.getArguments();
if (isOauthRequest && oe.getCode().equals("orcid.frontend.verify.duplicate_email")) {
// XXX
reg.getEmail().getErrors().add(getMessage("oauth.registration.duplicate_email", arguments));
} else if (oe.getCode().equals("orcid.frontend.verify.duplicate_email")) {
Object email = "";
if (arguments != null && arguments.length > 0) {
email = arguments[0];
}
String link = "/signin";
String linkType = reg.getLinkType();
if ("social".equals(linkType)) {
link = "/social/access";
} else if ("shibboleth".equals(linkType)) {
link = "/shibboleth/signin";
}
reg.getEmail().getErrors().add(getMessage(oe.getCode(), email, orcidUrlManager.getBaseUrl() + link));
} else if (oe.getCode().equals("orcid.frontend.verify.deactivated_email")) {
// Handle this message in angular to allow AJAX action
reg.getEmail().getErrors().add(oe.getCode());
} else {
reg.getEmail().getErrors().add(getMessage(oe.getCode(), oe.getArguments()));
}
}
// validate confirm if already field out
if (reg.getEmailConfirm().getValue() != null) {
regEmailConfirmValidate(reg);
}
return reg;
}
use of org.springframework.validation.ObjectError in project opennms by OpenNMS.
the class DefaultDistributedPollerService method resumeLocationMonitor.
/**
* {@inheritDoc}
*/
@Override
public void resumeLocationMonitor(LocationMonitorIdCommand command, BindingResult errors) {
if (command == null) {
throw new IllegalStateException("command argument cannot be null");
}
if (errors == null) {
throw new IllegalStateException("errors argument cannot be null");
}
if (errors.hasErrors()) {
return;
}
OnmsLocationMonitor monitor = m_locationMonitorDao.load(command.getMonitorId());
if (monitor.getStatus() != MonitorStatus.PAUSED) {
errors.addError(new ObjectError(MonitorStatus.class.getName(), new String[] { "distributed.locationMonitor.notPaused" }, new Object[] { command.getMonitorId() }, "Location monitor " + command.getMonitorId() + " is not paused."));
return;
}
monitor.setStatus(MonitorStatus.STARTED);
m_locationMonitorDao.update(monitor);
}
Aggregations