Search in sources :

Example 61 with FieldError

use of org.springframework.validation.FieldError 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 62 with FieldError

use of org.springframework.validation.FieldError in project spring-boot-starter-kit by tripsta.

the class ExceptionHandlingAdvisor method createErrorMessage.

private String createErrorMessage(ObjectError error) {
    StringBuilder errorMessage = new StringBuilder("");
    if (error instanceof FieldError) {
        FieldError fieldError = (FieldError) error;
        errorMessage.append("Validation failed: field [").append(fieldError.getField()).append("] ").append(error.getDefaultMessage());
    } else {
        errorMessage.append("[").append(error).append("] ");
    }
    return errorMessage.toString();
}
Also used : FieldError(org.springframework.validation.FieldError)

Example 63 with FieldError

use of org.springframework.validation.FieldError in project FP-PSP-SERVER by FundacionParaguaya.

the class ExceptionTranslatorAdvice method handleMethodArgumentNotValid.

/**
 * <p>
 *   Errors thrown in Controllers methods with arguments annotated with JAX-RS @Valid.
 * </p>
 *
 * @param ex
 * @return
 */
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    ErrorDTO dto = ErrorDTO.of(fieldErrors, ErrorCodes.VALIDATION_FAILURE.getMessage(), ex.getLocalizedMessage());
    return generateResponseEntity(dto, HttpStatus.BAD_REQUEST);
}
Also used : BindingResult(org.springframework.validation.BindingResult) FieldErrorDTO(py.org.fundacionparaguaya.pspserver.common.dtos.FieldErrorDTO) ErrorDTO(py.org.fundacionparaguaya.pspserver.common.dtos.ErrorDTO) FieldError(org.springframework.validation.FieldError)

Example 64 with FieldError

use of org.springframework.validation.FieldError in project jhipster-registry by jhipster.

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();
    ErrorVM dto = new ErrorVM(ErrorConstants.ERR_VALIDATION);
    for (FieldError fieldError : fieldErrors) {
        dto.add(fieldError.getObjectName(), fieldError.getField(), fieldError.getCode());
    }
    return dto;
}
Also used : BindingResult(org.springframework.validation.BindingResult) FieldError(org.springframework.validation.FieldError)

Example 65 with FieldError

use of org.springframework.validation.FieldError in project openmrs-core by openmrs.

the class HibernateAdministrationDAOTest method validate_shouldFailValidationForLocationClassIfFieldLengthsAreNotCorrect.

/**
 * @see HibernateAdministrationDAO#validate(Object,Errors)
 */
@Test
public void validate_shouldFailValidationForLocationClassIfFieldLengthsAreNotCorrect() {
    Location location = new Location();
    String longString = "too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text";
    String[] LocationFields = new String[] { "name", "description", "address1", "address2", "address3", "address4", "address5", "address6", "address7", "address8", "address9", "address10", "address11", "address12", "address13", "address14", "address15", "cityVillage", "stateProvince", "country", "postalCode", "latitude", "longitude", "countyDistrict", "retireReason" };
    String errorCode = "error.exceededMaxLengthOfField";
    location.setName(longString);
    location.setDescription(longString);
    location.setAddress1(longString);
    location.setAddress2(longString);
    location.setAddress3(longString);
    location.setAddress4(longString);
    location.setAddress5(longString);
    location.setAddress6(longString);
    location.setAddress7(longString);
    location.setAddress8(longString);
    location.setAddress9(longString);
    location.setAddress10(longString);
    location.setAddress11(longString);
    location.setAddress12(longString);
    location.setAddress13(longString);
    location.setAddress14(longString);
    location.setAddress15(longString);
    location.setCityVillage(longString);
    location.setStateProvince(longString);
    location.setCountry(longString);
    location.setPostalCode(longString);
    location.setLatitude(longString);
    location.setLongitude(longString);
    location.setCountyDistrict(longString);
    location.setRetireReason(longString);
    Errors errors = new BindException(location, "location");
    dao.validate(location, errors);
    for (String feilds : LocationFields) {
        FieldError fielderror = errors.getFieldError(feilds);
        Assert.assertTrue(errorCode.equals(fielderror.getCode()));
    }
}
Also used : Errors(org.springframework.validation.Errors) BindException(org.springframework.validation.BindException) FieldError(org.springframework.validation.FieldError) Location(org.openmrs.Location) Test(org.junit.Test) BaseContextSensitiveTest(org.openmrs.test.BaseContextSensitiveTest)

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