Example usage for org.springframework.validation BindingResult hasErrors

List of usage examples for org.springframework.validation BindingResult hasErrors

Introduction

In this page you can find the example usage for org.springframework.validation BindingResult hasErrors.

Prototype

boolean hasErrors();

Source Link

Document

Return if there were any errors.

Usage

From source file:org.zht.framework.validate.ValidateHandler.java

public static String getDefaultError(BindingResult result) {
    if (result.hasErrors()) {
        List<FieldError> fieldEist = result.getFieldErrors();
        if (fieldEist != null && fieldEist.size() > 0) {
            FieldError fieldError = fieldEist.get(0);
            fieldError.getCode();//from  w  w w. j a  v  a 2 s. c o  m
            String prefix = "\r\n" + "???";
            String value = "{" + (fieldError.getRejectedValue() == null ? " " : fieldError.getRejectedValue())
                    + "}";
            String message = "" + fieldError.getDefaultMessage();
            return prefix + "" + value + "\r\n" + message;
        } else {
            List<ObjectError> allErrorlist = result.getAllErrors();
            ObjectError oe = allErrorlist.get(0);
            return "" + oe.getDefaultMessage();
        }
    } else {
        return null;
    }
}

From source file:net.maritimecloud.identityregistry.utils.ValidateUtil.java

public static void hasErrors(BindingResult bindingResult, HttpServletRequest request)
        throws McBasicRestException {
    if (bindingResult.hasErrors()) {
        String combinedErrMsg = "";
        for (ObjectError err : bindingResult.getAllErrors()) {
            if (combinedErrMsg.length() != 0) {
                combinedErrMsg += ", ";
            }//from w  ww  . ja  v a 2s  .  c o m
            combinedErrMsg += err.getDefaultMessage();
        }
        throw new McBasicRestException(HttpStatus.BAD_REQUEST, combinedErrMsg, request.getServletPath());
    }
}

From source file:org.zht.framework.validate.ValidateHandler.java

public static ValidateResult handle(BindingResult result) {
    ValidateResult retVal = new ValidateResult();
    if (result.hasErrors()) {
        List<ObjectError> list = result.getAllErrors();
        ObjectError oe = list.get(0);//from  w w w .  j a v a2  s . com
        retVal.setMessage(oe.getDefaultMessage());
        retVal.setResult(false);
    } else {
        retVal.setResult(true);
    }
    return retVal;
}

From source file:common.web.controller.CommonActionsEmail.java

/**
 * executes an update, serves as a template method that incapsulates common logic for insert action
 * @param service/*from  w w w  .  java  2s.  co m*/
 * @param config
 * @param val for validation
 * @param req resulting model (i.e. errors, command objects ...) will be placed here
 */
public static void doInsert(IInsertServiceNotificationUser service, IControllerConfig config,
        ABindValidator val, HttpServletRequest req) {
    String action = req.getParameter(ControllerConfig.ACTION_PARAM_NAME);
    Object command = service.getInsertBean();
    RequestUtils.copyRequestAttributesFromMap(req, service.initInsert());

    if ("insert".equals(action)) {
        /** bind command */
        BindingResult res = val.bindAndValidate(command, req);
        if (res.hasErrors()) {
            //m.putAll(res.getModel());
            req.setAttribute(res.MODEL_KEY_PREFIX + config.getContentDataAttribute(), res);
            req.setAttribute(config.getContentDataAttribute(), command);
            common.CommonAttributes.addErrorMessage("form_errors", req);
            //return false;
        } else {
            service.insert(command);
            String server = RequestUtils.getFullServerPathHttp(req);
            //TODO set groups for getting advertisment from DB
            service.sendInsertNotificationUser(command, server, "site name", null);
            req.setAttribute(config.getContentDataAttribute(), service.getInsertBean());
            common.CommonAttributes.addHelpMessage("operation_succeed", req);
            //return true;
        }
    } else {
        req.setAttribute(config.getContentDataAttribute(), command);
    }
}

From source file:common.web.controller.CommonActions.java

/**
 * executes bind of parameters to command and selecting results that matches criteria
 * @param service/*from ww  w.j  a  v  a  2s .  co  m*/
 * @param config
 * @param val for validation
 * @param req resulting model (i.e. errors, command objects ...) will be placed here
 */
public static <T> void doFilteredSelect(IFilterService<T> service, IControllerConfig config, ABindValidator val,
        HttpServletRequest req) {
    RequestUtils.copyRequestAttributesFromMap(req, service.initFilter());
    IFilterBean<T> command = service.getFilterBean();

    BindingResult res = val.bindAndValidate(command, req);

    if (res.hasErrors()) {
        common.CommonAttributes.addErrorMessage("form_errors", req);
        //req.setAttribute(res.MODEL_KEY_PREFIX+config.getContentDataAttribute(), res);
        //req.setAttribute(config.getContentDataAttribute(), command);
    } else {
        //String action = req.getParameter(ControllerConfig.ACTION_PARAM_NAME);
        //if (action != null && action.equals("filter")){
        //   req.setAttribute(config.getContentDataAttribute(), command);
        //} else {
        req.setAttribute(config.getContentDataAttribute(), command.getItems(service));
        //}
    }
}

From source file:common.web.controller.CommonActions.java

/**
 * executes an update, serves as a template method that incapsulates common logic for insert action
 * @param service/*from  w ww  .  j  a va  2  s.  c om*/
 * @param config
 * @param val for validation
 * @param req resulting model (i.e. errors, command objects ...) will be placed here
 */
public static <T> boolean doInsert(IInsertService<T> service, IControllerConfig config, ABindValidator val,
        HttpServletRequest req) {
    String action = req.getParameter(ControllerConfig.ACTION_PARAM_NAME);
    T command = service.getInsertBean();
    RequestUtils.copyRequestAttributesFromMap(req, service.initInsert());

    if ("insert".equals(action)) {
        /** bind command */
        BindingResult res = val.bindAndValidate(command, req);
        if (res.hasErrors() || !service.insert(command)) {
            //m.putAll(res.getModel());
            req.setAttribute(res.MODEL_KEY_PREFIX + config.getContentDataAttribute(), res);
            req.setAttribute(config.getContentDataAttribute(), command);
            common.CommonAttributes.addErrorMessage("form_errors", req);
            return false;
        } else {
            //req.setAttribute(config.getContentDataAttribute(), service.getInsertBean());
            req.setAttribute(config.getContentDataAttribute(), command);
            common.CommonAttributes.addHelpMessage("operation_succeed", req);
        }
    } else {
        //val.bindAndValidate(command, req);
        req.setAttribute(config.getContentDataAttribute(), command);
    }
    return true;
}

From source file:common.web.controller.CommonActions.java

/**
 * executes an update, serves as a template method that incapsulates common logic for update action
 * @param service//  w ww  .ja  va  2 s  . c o m
 * @param config
 * @param val
 * @param req
 * @return false if id cannot be converted to long or where is no command with an appropriate id
 */
public static <T> boolean doUpdate(IUpdateService<T, Long> service, IControllerConfig config,
        ABindValidator val, HttpServletRequest req) {
    Long id = RequestUtils.getLongParam(req, "id");
    T command = null;
    if (id != null) {
        command = service.getUpdateBean(id);
    }
    if (id == null || command == null) {
        common.CommonAttributes.addErrorMessage("form_errors", req);
        return false;
    }

    String action = req.getParameter(ControllerConfig.ACTION_PARAM_NAME);
    RequestUtils.copyRequestAttributesFromMap(req, service.initUpdate());

    if ("update".equals(action)) {
        /** bind command */
        BindingResult res = val.bindAndValidate(command, req);
        if (res.hasErrors()) {
            //m.putAll(res.getModel());
            req.setAttribute(res.MODEL_KEY_PREFIX + config.getContentDataAttribute(), res);
            req.setAttribute(config.getContentDataAttribute(), command);
            common.CommonAttributes.addErrorMessage("form_errors", req);
        } else {
            service.update(command);
            req.setAttribute(config.getContentDataAttribute(), command);
            common.CommonAttributes.addHelpMessage("operation_succeed", req);
        }
    } else {
        req.setAttribute(config.getContentDataAttribute(), command);
    }
    return true;
}

From source file:us.repasky.microblog.controllers.UserControllerTest.java

private static final BindingResult getMockBindingResultWithError() {
    BindingResult bindingResult = mock(BindingResult.class);
    when(bindingResult.hasErrors()).thenReturn(true);
    return bindingResult;
}

From source file:us.repasky.microblog.controllers.UserControllerTest.java

private static final BindingResult getMockBindingResultWithoutError() {
    BindingResult bindingResult = mock(BindingResult.class);
    when(bindingResult.hasErrors()).thenReturn(false);
    return bindingResult;
}

From source file:org.opentides.util.CrudUtil.java

/**
 * Converts the binding error messages to list of MessageResponse
 * //from www . ja  v a 2s  . com
 * @param bindingResult
 */
public static List<MessageResponse> convertErrorMessage(BindingResult bindingResult, Locale locale,
        MessageSource messageSource) {
    List<MessageResponse> errorMessages = new ArrayList<MessageResponse>();
    if (bindingResult.hasErrors()) {
        for (ObjectError error : bindingResult.getAllErrors()) {
            MessageResponse message = null;
            if (error instanceof FieldError) {
                FieldError ferror = (FieldError) error;
                message = new MessageResponse(MessageResponse.Type.error, error.getObjectName(),
                        ferror.getField(), error.getCodes(), error.getArguments());
            } else
                message = new MessageResponse(MessageResponse.Type.error, error.getObjectName(), null,
                        error.getCodes(), error.getArguments());
            message.setMessage(messageSource.getMessage(message, locale));
            errorMessages.add(message);
        }
    }
    return errorMessages;
}