Example usage for org.springframework.validation FieldError getCode

List of usage examples for org.springframework.validation FieldError getCode

Introduction

In this page you can find the example usage for org.springframework.validation FieldError getCode.

Prototype

@Nullable
public String getCode() 

Source Link

Document

Return the default code of this resolvable, that is, the last one in the codes array.

Usage

From source file:org.jasig.schedassist.web.owner.schedule.AvailableBlockFormBackingObjectValidatorTest.java

/**
 * Leave start time uninitialized, assert 1 error.
 * /*from  w  ww.  j a  v  a  2 s .c o  m*/
 * @throws Exception
 */
@Test
public void testEmptyStartTime() throws Exception {
    AvailableBlockFormBackingObject fbo = new AvailableBlockFormBackingObject();
    Errors errors = new BindException(fbo, "command");

    AvailableBlockFormBackingObjectValidator validator = new AvailableBlockFormBackingObjectValidator();
    validator.validate(fbo, errors);

    List errorCodes = errors.getAllErrors();
    Assert.assertEquals(1, errorCodes.size());
    FieldError startTimeFieldError = errors.getFieldError("startTimePhrase");
    Assert.assertNotNull(startTimeFieldError);
    Assert.assertEquals("field.required", startTimeFieldError.getCode());
}

From source file:org.jasig.schedassist.web.owner.schedule.AvailableBlockFormBackingObjectValidatorTest.java

/**
 * Invalid end time, assert 1 error.//from w  w  w .j a  v a 2s  .c om
 * 
 * @throws Exception
 */
@Test
public void testInvalidEndTime() throws Exception {
    AvailableBlockFormBackingObject fbo = new AvailableBlockFormBackingObject();
    fbo.setStartTimePhrase("20090306-1200");
    fbo.setEndTimePhrase("abcde");
    Errors errors = new BindException(fbo, "command");

    AvailableBlockFormBackingObjectValidator validator = new AvailableBlockFormBackingObjectValidator();
    validator.validate(fbo, errors);
    List errorCodes = errors.getAllErrors();
    Assert.assertEquals(1, errorCodes.size());
    FieldError endTimeFieldError = errors.getFieldError("endTimePhrase");
    Assert.assertNotNull(endTimeFieldError);
    Assert.assertEquals("field.parseexception", endTimeFieldError.getCode());
}

From source file:org.jasig.schedassist.web.owner.schedule.AvailableBlockFormBackingObjectValidatorTest.java

/**
 * Invalid start time, assert 1 error./* w w w  .  j  a  v a  2  s .co m*/
 * 
 * @throws Exception
 */
@Test
public void testInvalidStartTime() throws Exception {
    AvailableBlockFormBackingObject fbo = new AvailableBlockFormBackingObject();
    fbo.setStartTimePhrase("abcde");
    Errors errors = new BindException(fbo, "command");

    AvailableBlockFormBackingObjectValidator validator = new AvailableBlockFormBackingObjectValidator();
    validator.validate(fbo, errors);
    List errorCodes = errors.getAllErrors();
    Assert.assertEquals(1, errorCodes.size());
    FieldError startTimeFieldError = errors.getFieldError("startTimePhrase");
    Assert.assertNotNull(startTimeFieldError);
    Assert.assertEquals("field.parseexception", startTimeFieldError.getCode());
}

From source file:org.codeqinvest.web.project.CreateProjectController.java

/**
 * This methods handles the submitted form for creating a new project.
 *//*from   w w w . j a v a 2  s.c om*/
@RequestMapping(value = "/create", method = RequestMethod.POST)
ModelAndView create(@ModelAttribute Project project, BindingResult bindingResult,
        @ModelAttribute("retrievedSonarProjectsAsJson") String sonarProjects, Model model) {

    projectConnectionsValidator.validate(project, bindingResult);
    if (bindingResult.hasErrors()) {
        log.info("Rejected creation of project due {} validation errors", bindingResult.getErrorCount());
        if (log.isDebugEnabled()) {
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                log.debug("Field {} has following error: {}", fieldError.getField(), fieldError.getCode());
            }
        }
        addDeserializedSonarProjectsToModel(sonarProjects, model);
        model.addAttribute("fieldErrors", bindingResult.getFieldErrors());
        return new ModelAndView("createProject");
    }

    // this little hack is necessary until a better way for binding the value from the form is found
    CodeChangeSettings codeChangeSettings = project.getCodeChangeSettings();
    if (codeChangeSettings.getMethod() == SupportedCodeChangeProbabilityMethod.COMMIT_BASED.getId()) {
        codeChangeSettings.setNumberOfCommits(codeChangeSettings.getDays());
        codeChangeSettings.setDays(null);
    }

    Project addedProject = projectRepository.save(project);
    analyzerScheduler.scheduleAnalyzer(addedProject);
    log.info("Created project {} and scheduled its quality analysis", project.getName());

    RedirectView redirect = new RedirectView("/projects/" + addedProject.getId());
    redirect.setExposeModelAttributes(false);
    return new ModelAndView(redirect);
}

From source file:org.jasig.schedassist.web.owner.schedule.AvailableBlockFormBackingObjectValidatorTest.java

/**
 * Both fields invalid, assert 2 error.//from w  ww . j  ava 2  s. c o  m
 * 
 * @throws Exception
 */
@Test
public void testInvalidBoth() throws Exception {
    AvailableBlockFormBackingObject fbo = new AvailableBlockFormBackingObject();
    fbo.setStartTimePhrase("abcde");
    fbo.setEndTimePhrase("abcde");
    Errors errors = new BindException(fbo, "command");

    AvailableBlockFormBackingObjectValidator validator = new AvailableBlockFormBackingObjectValidator();
    validator.validate(fbo, errors);
    List errorCodes = errors.getAllErrors();
    Assert.assertEquals(2, errorCodes.size());
    FieldError startTimeFieldError = errors.getFieldError("startTimePhrase");
    Assert.assertNotNull(startTimeFieldError);
    Assert.assertEquals("field.parseexception", startTimeFieldError.getCode());

    FieldError endTimeFieldError = errors.getFieldError("endTimePhrase");
    Assert.assertNotNull(endTimeFieldError);
    Assert.assertEquals("field.parseexception", endTimeFieldError.getCode());
}

From source file:org.ualerts.fixed.web.controller.AbstractFormController.java

private Map<String, List<String>> getFieldErrors(Errors errors) {
    if (!errors.hasFieldErrors())
        return null;

    Map<String, List<String>> fieldErrors = new HashMap<String, List<String>>();
    for (FieldError error : errors.getFieldErrors()) {
        if (!fieldErrors.containsKey(error.getField()))
            fieldErrors.put(error.getField(), new ArrayList<String>());
        fieldErrors.get(error.getField()).add(getMessage(error.getCode()));
    }//ww  w .ja  v  a2  s. co m
    return fieldErrors;
}

From source file:org.iwethey.forums.web.user.test.EditUserValidatorTest.java

public void testValidateLinkFormat() {
    User u = new User();
    BindException errors = new BindException(u, "editInfo");
    u.setNickname("ut_spork96");
    u.setUnencryptedPassword("itchy96");
    u.setPasswordCheck("itchy96");
    u.setIndentationFormat("nbsp");
    u.setHierarchyPosition("above");
    u.setLinkFormat("bad");

    mVal.validate(u, errors);//  www.  j  av a  2  s. c o  m

    assertEquals("linkFormat", 1, errors.getFieldErrorCount("linkFormat"));
    FieldError err = errors.getFieldError("linkFormat");
    assertEquals("link format", "error.link.format", err.getCode());

    errors = new BindException(u, "editInfo");
    u.setLinkFormat("split");
    mVal.validate(u, errors);

    assertEquals("linkFormat good", 0, errors.getFieldErrorCount("linkFormat"));
}

From source file:com.sra.biotech.submittool.persistence.client.SubmitExceptionHandler.java

@ExceptionHandler({ InvalidRequestException.class })
protected ResponseEntity<Object> handleInvalidRequest(RuntimeException e, WebRequest request) {
    InvalidRequestException ire = (InvalidRequestException) e;
    List<FieldErrorResource> fieldErrorResources = new ArrayList<>();

    List<FieldError> fieldErrors = ire.getErrors().getFieldErrors();
    for (FieldError fieldError : fieldErrors) {
        FieldErrorResource fieldErrorResource = new FieldErrorResource();
        fieldErrorResource.setResource(fieldError.getObjectName());
        fieldErrorResource.setField(fieldError.getField());
        fieldErrorResource.setCode(fieldError.getCode());
        fieldErrorResource.setMessage(fieldError.getDefaultMessage());
        fieldErrorResources.add(fieldErrorResource);
    }//  ww  w  . j  a v a  2  s .com

    ErrorResource error = new ErrorResource("InvalidRequest", ire.getMessage());
    error.setFieldErrors(fieldErrorResources);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    return handleExceptionInternal(e, error, headers, HttpStatus.UNPROCESSABLE_ENTITY, request);
}

From source file:com.iflytek.edu.cloud.frame.spring.DefaultHandlerExceptionResolver.java

/**
 * ??//  w w w. ja  v  a 2 s  . com
 *
 * @param allErrors
 * @param subErrorType1
 * @return
 */
private boolean hastSubErrorType(List<ObjectError> allErrors, SubErrorType subErrorType1) {
    for (ObjectError objectError : allErrors) {
        if (objectError instanceof FieldError) {
            FieldError fieldError = (FieldError) objectError;
            if (INVALIDE_CONSTRAINT_SUBERROR_MAPPINGS.containsKey(fieldError.getCode())) {
                SubErrorType tempSubErrorType = INVALIDE_CONSTRAINT_SUBERROR_MAPPINGS.get(fieldError.getCode());
                if (tempSubErrorType == subErrorType1) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:org.iwethey.forums.web.user.test.EditUserValidatorTest.java

public void testValidateImageFormat() {
    User u = new User();
    BindException errors = new BindException(u, "editInfo");
    u.setNickname("ut_spork96");
    u.setUnencryptedPassword("itchy96");
    u.setPasswordCheck("itchy96");
    u.setIndentationFormat("nbsp");
    u.setHierarchyPosition("above");
    u.setLinkFormat("inline");
    u.setImageFormat("bad");

    mVal.validate(u, errors);// w w w . ja va  2s  .co  m

    assertEquals("imageFormat", 1, errors.getFieldErrorCount("imageFormat"));
    FieldError err = errors.getFieldError("imageFormat");
    assertEquals("image format", "error.image.format", err.getCode());

    errors = new BindException(u, "editInfo");
    u.setImageFormat("inline");
    mVal.validate(u, errors);

    assertEquals("imageFormat good", 0, errors.getFieldErrorCount("imageFormat"));
}