Search in sources :

Example 46 with FieldError

use of org.springframework.validation.FieldError in project head by mifos.

the class LoanRepaymentFormBean method validateEnterLoanRepaymentDetails.

public void validateEnterLoanRepaymentDetails(ValidationContext context) {
    MessageContext messageContext = context.getMessageContext();
    Errors errors = validator.checkConstraints(this);
    if (errors.hasErrors()) {
        for (FieldError fieldError : errors.getFieldErrors()) {
            String arg = "";
            if ("paymentAmount".equals(fieldError.getField())) {
                arg = "Amount";
            }
            messageContext.addMessage(buildValidationMessage("errors.mandatory", fieldError.getField(), arg));
        }
    }
    if (dateValidator == null) {
        dateValidator = new DateValidator();
    }
    //payment date validation
    if (!dateValidator.formsValidDate(paymentDateDD, paymentDateMM, paymentDateYY)) {
        messageContext.addMessage(buildValidationMessage("errors.invaliddate", "paymentDate", "accounts.date_of_trxn"));
    } else if (getPaymentDate().isAfter(new LocalDate())) {
        messageContext.addMessage(buildValidationMessage("errors.futuredate", "paymentDate", "accounts.date_of_trxn"));
    } else if (getPaymentDate().isBefore(lastPaymentDate)) {
        messageContext.addMessage(buildValidationMessage("errors.payment.date.before.last.payment", "paymentDate", "accounts.date_of_trxn"));
    }
    //receipt date validation
    if (isReceiptDateSpecified() && !dateValidator.formsValidDate(receiptDateDD, receiptDateMM, receiptDateYY)) {
        messageContext.addMessage(buildValidationMessage("errors.invaliddate", "receiptDate", "accounts.receiptdate"));
    }
    //amount validation
    if (paymentAmount != null && paymentAmount.doubleValue() <= 0) {
        messageContext.addMessage(buildValidationMessage("error.penalty.incorrectDouble", "paymentAmount", "Amount"));
    }
    if (messageContext.hasErrorMessages()) {
        this.truePrintReceipt = this.printReceipt;
        this.printReceipt = false;
    }
}
Also used : DateValidator(org.mifos.clientportfolio.loan.ui.DateValidator) Errors(org.springframework.validation.Errors) FieldError(org.springframework.validation.FieldError) MessageContext(org.springframework.binding.message.MessageContext) LocalDate(org.joda.time.LocalDate)

Example 47 with FieldError

use of org.springframework.validation.FieldError in project head by mifos.

the class PentahoReportingController method addErrorToBindingResult.

private void addErrorToBindingResult(PentahoValidationError validationError, BindingResult bindingResult) {
    ObjectError error;
    if (validationError.isFieldError()) {
        error = new FieldError("pentahoReportFormBean", validationError.getParamName(), validationError.getParamName() + ": " + validationError.getErrorMessage());
    } else {
        error = new ObjectError("pentahoReportFormBean", validationError.getErrorMessage());
    }
    bindingResult.addError(error);
}
Also used : ObjectError(org.springframework.validation.ObjectError) FieldError(org.springframework.validation.FieldError)

Example 48 with FieldError

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

the class BindFailureAnalyzer method analyze.

@Override
protected FailureAnalysis analyze(Throwable rootFailure, BindException cause) {
    if (CollectionUtils.isEmpty(cause.getAllErrors())) {
        return null;
    }
    StringBuilder description = new StringBuilder(String.format("Binding to target %s failed:%n", cause.getTarget()));
    for (ObjectError error : cause.getAllErrors()) {
        if (error instanceof FieldError) {
            FieldError fieldError = (FieldError) error;
            description.append(String.format("%n    Property: %s", cause.getObjectName() + "." + fieldError.getField()));
            description.append(String.format("%n    Value: %s", fieldError.getRejectedValue()));
        }
        description.append(String.format("%n    Reason: %s%n", error.getDefaultMessage()));
    }
    return new FailureAnalysis(description.toString(), "Update your application's configuration", cause);
}
Also used : ObjectError(org.springframework.validation.ObjectError) FieldError(org.springframework.validation.FieldError) FailureAnalysis(org.springframework.boot.diagnostics.FailureAnalysis)

Example 49 with FieldError

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

the class RelaxedDataBinderTests method testRequiredFieldsValidation.

@Test
public void testRequiredFieldsValidation() throws Exception {
    TargetWithValidatedMap target = new TargetWithValidatedMap();
    BindingResult result = bind(target, "info[foo]: bar");
    assertThat(result.getErrorCount()).isEqualTo(2);
    for (FieldError error : result.getFieldErrors()) {
        System.err.println(new StaticMessageSource().getMessage(error, Locale.getDefault()));
    }
}
Also used : BindingResult(org.springframework.validation.BindingResult) StaticMessageSource(org.springframework.context.support.StaticMessageSource) FieldError(org.springframework.validation.FieldError) Test(org.junit.Test)

Example 50 with FieldError

use of org.springframework.validation.FieldError in project ArachneCentralAPI by OHDSI.

the class BaseSkillController method create.

@ApiOperation("Register new skill.")
@RequestMapping(value = "/api/v1/user-management/skills", method = RequestMethod.POST)
public JsonResult<SkillDTO> create(@RequestBody @Valid SkillDTO skillDTO, BindingResult binding) throws NotExistException, NotUniqueException, PermissionDeniedException {
    JsonResult<SkillDTO> result;
    if (binding.hasErrors()) {
        result = new JsonResult<>(JsonResult.ErrorCode.VALIDATION_ERROR);
        for (FieldError fieldError : binding.getFieldErrors()) {
            result.getValidatorErrors().put(fieldError.getField(), fieldError.getDefaultMessage());
        }
    } else {
        S skill = convertDtoToSkill(skillDTO);
        skill = skillService.create(skill);
        result = new JsonResult<>(JsonResult.ErrorCode.NO_ERROR);
        result.setResult(conversionService.convert(skill, SkillDTO.class));
    }
    return result;
}
Also used : SkillDTO(com.odysseusinc.arachne.portal.api.v1.dto.dictionary.SkillDTO) FieldError(org.springframework.validation.FieldError) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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