Example usage for org.springframework.validation BindingResult getFieldError

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

Introduction

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

Prototype

@Nullable
FieldError getFieldError();

Source Link

Document

Get the first error associated with a field, if any.

Usage

From source file:com.wiiyaya.consumer.web.main.controller.PrivilegeController.java

@RequestMapping(value = MainURIResource.PATH_SYS_PRIVILEGE_ADD, method = { RequestMethod.POST })
@ResponseBody//from ww w  .j  a v  a 2 s.c om
public PrivilegeDto add(
        @Validated @ModelAttribute(MainURIResource.MODEL_PRIVILEGE_DTO) PrivilegeDto privilegeDto,
        BindingResult result) throws ValidateException, BusinessException {
    if (result.hasErrors()) {
        throw new ValidateException(result.getFieldError().getObjectName(), result.getFieldError().getField(),
                result.getFieldError().getDefaultMessage());
    }
    return privilegeService.savePrivilege(privilegeDto);
}

From source file:md.ibanc.rm.controllers.request.RequestCardsController.java

private ResponseEntity<CardsDetails> showFieldErrors(BindingResult bindingResult) {
    CardsDetails cardsDetails = new CardsDetails();

    switch (bindingResult.getFieldError().getField()) {
    case "userName": {
        cardsDetails.setReturnCode(RetCodeConst.FIELD_VALIDATION_ERROR);
        cardsDetails.setReturnDescription(RetDescriptionConst.USERNAME_VALIDATION_ERROR);
    }//from w  ww .  ja  va2  s . c o  m
        break;
    case "GUID": {
        cardsDetails.setReturnCode(RetCodeConst.FIELD_VALIDATION_ERROR);
        cardsDetails.setReturnDescription(RetDescriptionConst.GUID_VALIDATION_ERROR);
    }
        break;
    default: {
        cardsDetails.setReturnCode(RetCodeConst.UNKNOW_FIELD_ERROR);
        cardsDetails.setReturnDescription(RetDescriptionConst.UNKNOW_FIELD_VALIDATION_ERROR);
    }
    }
    return new ResponseEntity<>(cardsDetails, HttpStatus.BAD_REQUEST);
}

From source file:com.wiiyaya.consumer.web.main.controller.RoleController.java

@RequestMapping(value = MainURIResource.PATH_SYS_ROLE_ADD, method = { RequestMethod.POST })
@ResponseBody/*from w w w.ja va  2s .  c o m*/
public void add(@Validated @ModelAttribute(MainURIResource.MODEL_ROLE_DTO) RoleDto roleDto,
        BindingResult result) throws ValidateException, BusinessException {
    if (result.hasErrors()) {
        throw new ValidateException(result.getFieldError().getObjectName(), result.getFieldError().getField(),
                result.getFieldError().getDefaultMessage());
    }
    roleService.saveRole(roleDto);
}

From source file:com.wiiyaya.consumer.web.main.controller.RoleController.java

@RequestMapping(value = MainURIResource.PATH_SYS_ROLE_AMD, method = { RequestMethod.PUT })
@ResponseBody//  w  w  w . ja v a2s . co  m
public void amend(@Validated @ModelAttribute(MainURIResource.MODEL_ROLE_DTO) RoleDto roleDto,
        BindingResult result) throws ValidateException, BusinessException {
    if (result.hasErrors()) {
        throw new ValidateException(result.getFieldError().getObjectName(), result.getFieldError().getField(),
                result.getFieldError().getDefaultMessage());
    }
    roleService.updateRole(roleDto);
}

From source file:com.wiiyaya.consumer.web.main.controller.UserController.java

@RequestMapping(value = MainURIResource.PATH_SYS_USER_ADD, method = { RequestMethod.POST })
@ResponseBody/*w ww . jav a  2 s .  c  o m*/
public void add(
        @Validated @ModelAttribute(MainURIResource.MODEL_BACKEND_USER_DTO) BackendUserDto backendUserDto,
        BindingResult result) throws BusinessException, ValidateException {
    if (result.hasErrors()) {
        throw new ValidateException(result.getFieldError().getObjectName(), result.getFieldError().getField(),
                result.getFieldError().getDefaultMessage());
    }
    backendUserService.saveUser(backendUserDto);
}

From source file:com.wiiyaya.consumer.web.main.controller.UserController.java

@RequestMapping(value = MainURIResource.PATH_SYS_USER_AMD, method = { RequestMethod.PUT })
@ResponseBody//from   www.jav  a 2s . co  m
public void amend(
        @Validated @ModelAttribute(MainURIResource.MODEL_BACKEND_USER_DTO) BackendUserDto backendUserDto,
        BindingResult result) throws ValidateException, BusinessException {
    if (result.hasErrors()) {
        throw new ValidateException(result.getFieldError().getObjectName(), result.getFieldError().getField(),
                result.getFieldError().getDefaultMessage());
    }
    backendUserService.updateUser(backendUserDto);
}

From source file:br.com.modoagil.asr.rest.support.RESTErrorHandler.java

/**
 * Manipula exceo de validao de objetos nos servios
 *
 * @param ex/*from   w  w w .  ja v a  2 s  .c  o m*/
 *            {@link MethodArgumentNotValidException}
 * @return resposta ao cliente
 */
@ResponseBody
@ExceptionHandler(MethodArgumentNotValidException.class)
public Response<E> processMethodArgumentNotValidException(final MethodArgumentNotValidException ex) {
    this.logger.info("handleMethodArgumentNotValidException - Catching: " + ex.getClass().getSimpleName(), ex);
    final BindingResult result = ex.getBindingResult();
    final FieldError fieldError = result.getFieldError();
    final String message = this.messageSource.getMessage(fieldError.getDefaultMessage(), null, null);
    return new ResponseBuilder<E>().success(false).status(HttpStatus.BAD_REQUEST).message(message).build();
}

From source file:br.com.semanticwot.cd.controllers.GatewayController.java

@RequestMapping(method = RequestMethod.POST, name = "saveGateway")
public ModelAndView save(@Valid @ModelAttribute("gateway") GatewayForm gatewayForm, BindingResult bindingResult,
        RedirectAttributes redirectAttributes, Authentication authentication) {

    if (bindingResult.hasErrors()) {
        System.out.println(bindingResult.getFieldError().getField());
        System.out.println(bindingResult.getFieldError().getDefaultMessage());
        System.out.println("ERRO FORM");
        return form(gatewayForm, authentication);
    }/*  ww  w .j  a v  a2s  .c o m*/

    // Instalacao do gateway
    // 1) verificar envio de arquivo grande, e analisar o comportamento do navegador
    SSHManager sSHManager = new SSHManager(gatewayForm.getUser(), gatewayForm.getPassword(),
            gatewayForm.getIp(), "");

    String errorMessage = sSHManager.connect();

    if (errorMessage != null) {
        System.out.println(errorMessage);
    }

    // Talvez usar server sent events aqui, mais nao quero perder mais tempo

    try {
        sSHManager.sendFile(System.getProperty("user.home") + System.getProperty("file.separator")
                + Constants.PSWOT_GATEWAY_PATH + System.getProperty("file.separator")
                + Constants.PSWOT_GATEWAY_FILE, "/tmp/novo_host.zip", TransferProtocolType.SFTP);
    } catch (RuntimeException ex) {
        throw new GatewayWotNotCreated("Gateway WoT not installed, try again.");
    }

    sSHManager.close();

    System.out.println("Imprimindo no SAVE " + gatewayForm.toString());

    ModelAndView modelAndView = new ModelAndView("redirect:/#install");
    redirectAttributes.addFlashAttribute("info", "Gateway WoT successful install");
    return modelAndView;
}

From source file:mylife.web.userController.java

/**
 *
 * @param user//from   w w  w.  j  av  a  2  s. co  m
 * @param result
 * @param request
 * @return
 */
@RequestMapping(value = "/user/editsave", method = RequestMethod.POST)
public ModelAndView editsave(@ModelAttribute("user") @Valid user user, BindingResult result,
        HttpServletRequest request) {
    if (result.hasErrors()) {
        logger.info(result.getFieldError().toString());
        return new ModelAndView("usereditform", "user", user);
    }

    int r = dao.update(user);

    Message msg = null;
    if (r == 1) {
        msg = new Message(Message.Level.INFO, "User has been successfully saved");
    } else {
        msg = new Message(Message.Level.ERROR, "Edit user failed");
    }

    request.getSession().setAttribute("message", msg);

    return new ModelAndView("redirect:/user/viewuser");
}

From source file:mylife.web.interactionsController.java

/**
 *
 * @param interactions//  ww  w  . java 2s.co  m
 * @param result
 * @param request
 * @return
 */
@RequestMapping(value = "/interactions/editsave", method = RequestMethod.POST)
public ModelAndView editsave(@ModelAttribute("interactions") @Valid interactions interactions,
        BindingResult result, HttpServletRequest request) {

    if (result.hasErrors()) {
        logger.info(result.getFieldError().toString());
        return new ModelAndView("interactionseditform", "interactions", interactions);
    }

    int r = dao.update(interactions);

    Message msg = null;
    if (r == 1) {
        msg = new Message(Message.Level.INFO, "Interactions has been successfully saved");
    } else {
        msg = new Message(Message.Level.ERROR, "Edit interactions failed");
    }

    request.getSession().setAttribute("message", msg);

    return new ModelAndView("redirect:/interactions/viewinteractions");
}