Search in sources :

Example 76 with FieldError

use of org.springframework.validation.FieldError in project tutorials by eugenp.

the class ExceptionTranslator method processValidationError.

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorVM processValidationError(MethodArgumentNotValidException ex) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    return processFieldErrors(fieldErrors);
}
Also used : BindingResult(org.springframework.validation.BindingResult) FieldError(org.springframework.validation.FieldError)

Example 77 with FieldError

use of org.springframework.validation.FieldError in project tutorials by eugenp.

the class CustomRestExceptionHandler method handleBindException.

@Override
protected ResponseEntity<Object> handleBindException(final BindException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
    logger.info(ex.getClass().getName());
    // 
    final List<String> errors = new ArrayList<String>();
    for (final FieldError error : ex.getBindingResult().getFieldErrors()) {
        errors.add(error.getField() + ": " + error.getDefaultMessage());
    }
    for (final ObjectError error : ex.getBindingResult().getGlobalErrors()) {
        errors.add(error.getObjectName() + ": " + error.getDefaultMessage());
    }
    final ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), errors);
    return handleExceptionInternal(ex, apiError, headers, apiError.getStatus(), request);
}
Also used : ObjectError(org.springframework.validation.ObjectError) ArrayList(java.util.ArrayList) FieldError(org.springframework.validation.FieldError)

Example 78 with FieldError

use of org.springframework.validation.FieldError in project spring-boot by Linda-Tan.

the class GlobalExceptionHandler method MethodArgumentNotValidHandler.

@ExceptionHandler(value = MethodArgumentNotValidException.class)
public BaseResponse MethodArgumentNotValidHandler(MethodArgumentNotValidException exception) {
    List invalidArguments = new ArrayList<>();
    // 解析原错误信息,封装后返回,此处返回非法的字段名称,原始值,错误信息
    for (FieldError error : exception.getBindingResult().getFieldErrors()) {
        Set invalidArgument = new HashSet(3);
        invalidArgument.add(error.getDefaultMessage());
        invalidArgument.add(error.getField());
        invalidArgument.add(error.getRejectedValue());
        invalidArguments.add(invalidArgument);
    }
    return new ObjectRestResponse(StatusCodeEnum.EX_PRAM_ERROR, invalidArguments);
}
Also used : FieldError(org.springframework.validation.FieldError) ObjectRestResponse(com.junliang.spring.pojo.vo.ObjectRestResponse) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 79 with FieldError

use of org.springframework.validation.FieldError in project arch-playground by BeneStem.

the class BadRequestException method badRequest.

public static BadRequestException badRequest(final Object target, final String targetName, final String fieldName, final String message) {
    final var error = new BeanPropertyBindingResult(target, targetName);
    error.addError(new FieldError(targetName, fieldName, message));
    return badRequest(error);
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) FieldError(org.springframework.validation.FieldError)

Example 80 with FieldError

use of org.springframework.validation.FieldError in project jprime by bgjug.

the class CfpController method validateEmail.

private String validateEmail(BindingResult bindingResult, Submission submission, Model model, String email, String role) {
    EmailValidator emailValidator = new EmailValidator();
    if (!StringUtils.isEmpty(email) && emailValidator.isValid(email, null)) {
        // Email is valid
        return null;
    }
    bindingResult.addError(new FieldError("submission", role + ".email", "Invalid Email!"));
    return goToCFP(submission, model);
}
Also used : EmailValidator(org.hibernate.validator.internal.constraintvalidators.bv.EmailValidator) FieldError(org.springframework.validation.FieldError)

Aggregations

FieldError (org.springframework.validation.FieldError)92 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)20 ObjectError (org.springframework.validation.ObjectError)19 Test (org.junit.jupiter.api.Test)17 BindingResult (org.springframework.validation.BindingResult)16 ApiOperation (io.swagger.annotations.ApiOperation)14 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)12 Errors (org.springframework.validation.Errors)11 ArrayList (java.util.ArrayList)7 BindException (org.springframework.validation.BindException)6 CustomResult (com.megagao.production.ssm.domain.customize.CustomResult)5 Locale (java.util.Locale)5 Test (org.junit.Test)5 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)5 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)5 JsonResult (com.odysseusinc.arachne.commons.api.v1.dto.util.JsonResult)4 HashMap (java.util.HashMap)4 UserProfileDTO (com.odysseusinc.arachne.portal.api.v1.dto.UserProfileDTO)3 LinkedHashSet (java.util.LinkedHashSet)3 MapBindingResult (org.springframework.validation.MapBindingResult)3