Search in sources :

Example 66 with ObjectError

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);
}
Also used : BindingResult(org.springframework.validation.BindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) ObjectError(org.springframework.validation.ObjectError) BindException(org.springframework.validation.BindException) MapBindingResult(org.springframework.validation.MapBindingResult) ServletException(javax.servlet.ServletException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) BindException(org.springframework.validation.BindException) Test(org.junit.Test)

Example 67 with ObjectError

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());
}
Also used : LocationMonitorIdCommand(org.opennms.web.svclayer.model.LocationMonitorIdCommand) ObjectError(org.springframework.validation.ObjectError) BindException(org.springframework.validation.BindException) OnmsLocationMonitor(org.opennms.netmgt.model.OnmsLocationMonitor)

Example 68 with ObjectError

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());
}
Also used : LocationMonitorIdCommand(org.opennms.web.svclayer.model.LocationMonitorIdCommand) ObjectError(org.springframework.validation.ObjectError) BindException(org.springframework.validation.BindException)

Example 69 with ObjectError

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;
}
Also used : ObjectError(org.springframework.validation.ObjectError) MapBindingResult(org.springframework.validation.MapBindingResult) FieldError(org.springframework.validation.FieldError)

Example 70 with ObjectError

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);
}
Also used : ObjectError(org.springframework.validation.ObjectError) OnmsLocationMonitor(org.opennms.netmgt.model.OnmsLocationMonitor)

Aggregations

ObjectError (org.springframework.validation.ObjectError)106 FieldError (org.springframework.validation.FieldError)19 BindingResult (org.springframework.validation.BindingResult)17 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)17 BindException (org.springframework.validation.BindException)14 ArrayList (java.util.ArrayList)13 MapBindingResult (org.springframework.validation.MapBindingResult)13 Test (org.junit.jupiter.api.Test)12 ModelAndView (org.springframework.web.servlet.ModelAndView)10 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)7 Test (org.junit.Test)6 BusinessRuleException (org.mifos.service.BusinessRuleException)6 Errors (org.springframework.validation.Errors)6 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)6 DocumentBuilder (javax.xml.parsers.DocumentBuilder)5 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)5 OnmsLocationMonitor (org.opennms.netmgt.model.OnmsLocationMonitor)5 LocationMonitorIdCommand (org.opennms.web.svclayer.model.LocationMonitorIdCommand)5 Document (org.w3c.dom.Document)5 Element (org.w3c.dom.Element)5