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:md.ibanc.rm.controllers.request.RequestCustomersController.java

private ResponseEntity<CustomersDetails> showFieldErrors(BindingResult bindingResult) {
    CustomersDetails customersDetails = new CustomersDetails();

    switch (bindingResult.getFieldError().getField()) {
    case "mEmail": {
        customersDetails.setReturnCode(RetCodeConst.FIELD_VALIDATION_ERROR);
        customersDetails.setReturnDescription(RetDescriptionConst.EMAIL_VALIDATION_ERROR);
    }//from w  w w  . j a va2 s  .c o m
        break;
    case "mPassword": {
        customersDetails.setReturnCode(RetCodeConst.FIELD_VALIDATION_ERROR);
        customersDetails.setReturnDescription(RetDescriptionConst.PASSWORD_VALIDATION_ERROR);
    }
        break;
    default: {
        customersDetails.setReturnCode(RetCodeConst.UNKNOW_FIELD_ERROR);
        customersDetails.setReturnDescription(RetDescriptionConst.UNKNOW_FIELD_VALIDATION_ERROR);
    }
    }
    return new ResponseEntity<>(customersDetails, HttpStatus.BAD_REQUEST);
}

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

/**
 * ?/*from  www . jav  a2s  .  co  m*/
 * @param resourceDto ??
 * @param result ?
 * @throws ValidateException
 * @throws BusinessException
 */
@RequestMapping(value = MainURIResource.PATH_SYS_RESOURCE_ADD, method = { RequestMethod.POST })
@ResponseBody
public void add(@Validated @ModelAttribute(MainURIResource.MODEL_RESOURCE_DTO) ResourceDto resourceDto,
        BindingResult result) throws ValidateException, BusinessException {
    if (result.hasErrors()) {
        throw new ValidateException(result.getFieldError().getObjectName(), result.getFieldError().getField(),
                result.getFieldError().getDefaultMessage());
    }
    resourceService.saveResource(resourceDto);
}

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

/**
 * ?//from   ww w .  ja  v a 2  s  .c o m
 * @param resourceDto ??
 * @param result ?
 * @throws ValidateException
 * @throws BusinessException
 */
@RequestMapping(value = MainURIResource.PATH_SYS_RESOURCE_AMD, method = { RequestMethod.PUT })
@ResponseBody
public void amd(@Validated @ModelAttribute(MainURIResource.MODEL_RESOURCE_DTO) ResourceDto resourceDto,
        BindingResult result) throws ValidateException, BusinessException {
    if (result.hasErrors()) {
        throw new ValidateException(result.getFieldError().getObjectName(), result.getFieldError().getField(),
                result.getFieldError().getDefaultMessage());
    }
    resourceService.updateResource(resourceDto);
}

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

/**
 * ???/*  w ww.j  a v  a  2 s .c  om*/
 * @param resourceDto ??
 * @param result ?
 * @throws ValidateException
 * @throws BusinessException
 */
@RequestMapping(value = MainURIResource.PATH_SYS_RESOURCE_AUTH_AMD, method = { RequestMethod.PUT })
@ResponseBody
public void authAmd(@Validated @ModelAttribute(MainURIResource.MODEL_RESOURCE_DTO) ResourceDto resourceDto,
        BindingResult result) throws ValidateException, BusinessException {
    if (result.hasErrors()) {
        throw new ValidateException(result.getFieldError().getObjectName(), result.getFieldError().getField(),
                result.getFieldError().getDefaultMessage());
    }
    resourceService.updateResourceAuth(resourceDto);
}

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

/**
 * ?/* ww  w.j  a  v  a 2s .  co  m*/
 * @param resourceDto ??
 * @param result ?
 * @throws ValidateException
 * @throws BusinessException
 */
@RequestMapping(value = MainURIResource.PATH_SYS_RESOURCE_CONTENT_AMD, method = { RequestMethod.PUT })
@ResponseBody
public void contentAmd(@Validated @ModelAttribute(MainURIResource.MODEL_RESOURCE_DTO) ResourceDto resourceDto,
        BindingResult result) throws ValidateException, BusinessException {
    if (result.hasErrors()) {
        throw new ValidateException(result.getFieldError().getObjectName(), result.getFieldError().getField(),
                result.getFieldError().getDefaultMessage());
    }
    resourceService.updateResourceContent(resourceDto);
}

From source file:com.faxintong.iruyi.controller.lawyer.UserController.java

/**
 * /*from   w ww .ja  va  2  s  .  c  o m*/
 * @param lawyer
 * @param bindingResult
 * @param validCode
 * @param request
 * @param response
 * @return
 */
@RequestMapping(value = "reg")
public Map<String, Object> register(@Valid Lawyer lawyer, BindingResult bindingResult, String validCode,
        HttpServletRequest request, HttpServletResponse response) {
    Map<String, Object> result = Maps.newHashMap();
    result.put(RESULT, false);
    try {
        if (bindingResult.hasErrors()) {
            result.put(ERR_MSG, bindingResult.getFieldError().getDefaultMessage());
        } else {
            String code = "123";//(String) request.getSession().getAttribute("code");
            if (code == null) {
                result.put(ERR_MSG, "??");
            } else if (!code.equals(validCode)) {
                result.put(ERR_MSG, "??");
            } else if (userService.containsPhone(lawyer.getPhone())) {
                result.put(ERR_MSG, "??");
            } else if (!userService.regisValidate(lawyer.getPhone())) {
                result.put(ERR_MSG, "??");
            } else {
                lawyer.setPassword(MD5.newinstance().getMD5ofStr(lawyer.getPassword()));
                Date date = new Date();
                lawyer.setCreateDate(date);
                lawyer.setUpdateDate(date);
                userService.registerLawyer(lawyer);
                result.put(RESULT, true);
                result.put(ERR_MSG, "?");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(":" + e.getMessage());
        result.put(ERR_MSG, "!");
    }
    return result;
}

From source file:cs544.letmegiveexam.controller.UserExamController.java

@RequestMapping(value = "/populateResult/{Id}/{setId}", method = RequestMethod.POST)
public String sumbitExam(@Valid QuestionSet questionSet, BindingResult result, HttpSession session,
        @PathVariable long Id, @PathVariable long setId) {
    UserExam userExam = userExamService.get(Id);
    if (!result.hasErrors()) {

        QuestionSet questionSetDB = questionSetService.get(setId);
        List<Question> questionList = questionSetDB.getQuestionslist();
        for (int i = 0; i < questionList.size(); i++) {
            Question que = questionList.get(i);
            que.setUserAnswer(questionSet.getQuestionslist().get(i).getUserAnswer());
        }// w w w  .j av  a 2 s  .  c  om

        int score = calcualteResult(questionList);
        //System.out.println("Score:" + score);
        userExam.setScore(score);

        //calculate the duration of exam
        //Timestamp ts_now = new Timestamp(());
        long durat = (Long) ExamDuration.get(userExam.getStartTime());
        userExam.setDuration(Long.toString(durat));
        userExamService.update(userExam);
    } else {
        System.out.println("i m here" + result.getFieldError());
    }
    return "redirect:/examHistory";
}

From source file:com.att.pirates.controller.ProjectController.java

@RequestMapping(value = "/updateProjectInfo/{prismId}", method = RequestMethod.POST)
public String updateProjectInfo(@PathVariable("prismId") String prismId,
        @Valid @ModelAttribute("projectInfo") InsightProject projectInfo, BindingResult result) {
    if (result.hasErrors()) {
        logger.error("There are " + result.getErrorCount() + " errors..");
        FieldError er = result.getFieldError();
        logger.error("Field: " + er.getField() + " ErrorMsg: " + er.getDefaultMessage());
    } else {//from w  w w  .ja  v  a  2 s  .  c  om
        String es = HtmlUtils.htmlEscape(projectInfo.getProjectDescription());

        String status = projectInfo.getProjectStatus();
        String release = projectInfo.getReleaseName();
        String startdate = projectInfo.getProjectStartDate();
        String dueDate = projectInfo.getProjectDueDate();
        // lookup statusid and releaseid
        int statusId = DataService.getStatusIdByname(status);
        int releaseId = DataService.getReleaseIdByName(release);

        InsightProject prj = new InsightProject();
        prj.setPrismId(prismId);
        prj.setLeadPM(projectInfo.getLeadPM());
        prj.setProjectDescription(es);
        prj.setProjectDueDate(dueDate);
        prj.setProjectName(HtmlUtils.htmlEscape(projectInfo.getProjectName()));
        prj.setProjectStartDate(startdate);
        prj.setStatudId(statusId);
        prj.setProjectStatus(status);
        prj.setReleaseName(release);
        prj.setReleaseId(releaseId);

        boolean rc = DataService.updateProjectInfo(prj);
        if (!rc) {
            logger.error("DataService.updateProjectInfo(prj); failed");
        }
    }
    // prismId will be considered while expanding the placeholders
    // return "redirect:/impact_pirates/projects/index/{prismId}";
    return "redirect:/projects/index/{prismId}";
}

From source file:edu.zipcloud.cloudstreetmarket.core.util.ValidatorUtil.java

public static void raiseFirstError(BindingResult result) {
    if (result.hasFieldErrors()) {
        throw new IllegalArgumentException(result.getFieldError().getDefaultMessage());
    } else if (result.hasGlobalErrors()) {
        throw new IllegalArgumentException(result.getGlobalError().getDefaultMessage());
    } else if (result.hasErrors()) {
        throw new IllegalArgumentException(result.getAllErrors().get(0).getCode());
    }/*from   w  w w. jav a 2  s.com*/
}