Example usage for org.springframework.validation FieldError getCode

List of usage examples for org.springframework.validation FieldError getCode

Introduction

In this page you can find the example usage for org.springframework.validation FieldError getCode.

Prototype

@Nullable
public String getCode() 

Source Link

Document

Return the default code of this resolvable, that is, the last one in the codes array.

Usage

From source file:org.jasig.schedassist.web.owner.schedule.BlockBuilderFormBackingObjectValidatorTest.java

/**
 * Test invalid value for daysOfWeekPhrase.
 * //  w w  w.j  av  a2s . c  om
 * @throws Exception
 */
@Test
public void testInvalidDaysOfWeek() throws Exception {
    BlockBuilderFormBackingObject fbo = new BlockBuilderFormBackingObject();
    fbo.setDaysOfWeekPhrase("NMTWRFX");
    fbo.setStartTimePhrase("9:11 AM");
    fbo.setEndTimePhrase("3:00 PM");
    fbo.setStartDatePhrase("1/1/2009");
    fbo.setEndDatePhrase("6/1/2009");
    fbo.setVisitorsPerAppointment(1);

    Errors errors = new BindException(fbo, "command");

    BlockBuilderFormBackingObjectValidator validator = new BlockBuilderFormBackingObjectValidator();
    validator.validate(fbo, errors);
    List errorCodes = errors.getAllErrors();
    Assert.assertEquals(1, errorCodes.size());
    FieldError daysFieldError = errors.getFieldError("daysOfWeekPhrase");
    Assert.assertNotNull(daysFieldError);
    Assert.assertEquals("invalid.daysOfWeekPhrase", daysFieldError.getCode());
}

From source file:org.jasig.schedassist.web.owner.schedule.BlockBuilderFormBackingObjectValidatorTest.java

/**
 * Test invalid values for endTimePhrase.
 * //from   w  w w . ja v  a 2 s.  c  o  m
 * @throws Exception
 */
@Test
public void testInvalidEndTime() throws Exception {
    BlockBuilderFormBackingObject fbo = new BlockBuilderFormBackingObject();
    fbo.setDaysOfWeekPhrase("NMTWRFS");
    fbo.setStartTimePhrase("9:33 AM");
    fbo.setEndTimePhrase("13:00 PM");
    fbo.setStartDatePhrase("1/1/2009");
    fbo.setEndDatePhrase("6/1/2009");
    fbo.setVisitorsPerAppointment(1);

    Errors errors = new BindException(fbo, "command");

    BlockBuilderFormBackingObjectValidator validator = new BlockBuilderFormBackingObjectValidator();
    validator.validate(fbo, errors);
    List errorCodes = errors.getAllErrors();
    Assert.assertEquals(1, errorCodes.size());
    FieldError endTimeError = errors.getFieldError("endTimePhrase");
    Assert.assertNotNull(endTimeError);
    Assert.assertEquals("field.militarytime", endTimeError.getCode());

    fbo.setEndTimePhrase("abcde");
    errors = new BindException(fbo, "command");
    validator.validate(fbo, errors);
    Assert.assertEquals(1, errorCodes.size());
    FieldError endTimeError2 = errors.getFieldError("endTimePhrase");
    Assert.assertNotNull(endTimeError2);
    Assert.assertEquals("field.timeparseexception", endTimeError2.getCode());

    // reset fields for testing AVAIL-117
    fbo.setDaysOfWeekPhrase("NMTWRFS");
    fbo.setStartTimePhrase("9:44 AM");
    fbo.setEndTimePhrase("10:65 AM");
    fbo.setStartDatePhrase("1/1/2009");
    fbo.setEndDatePhrase("6/1/2009");
    fbo.setVisitorsPerAppointment(1);

    errors = new BindException(fbo, "command");
    validator.validate(fbo, errors);
    errorCodes = errors.getAllErrors();
    Assert.assertEquals(1, errorCodes.size());
    endTimeError2 = errors.getFieldError("endTimePhrase");
    Assert.assertNotNull(endTimeError2);
    Assert.assertEquals("field.timeparseexception", endTimeError2.getCode());
}

From source file:org.jasig.schedassist.web.owner.schedule.BlockBuilderFormBackingObjectValidatorTest.java

/**
 * Test invalid values for startTimePhrase.
 * /*w  w w . j av  a  2s.  c  o m*/
 * @throws Exception
 */
@Test
public void testInvalidStartTime() throws Exception {
    BlockBuilderFormBackingObject fbo = new BlockBuilderFormBackingObject();
    fbo.setDaysOfWeekPhrase("NMTWRFS");
    fbo.setStartTimePhrase("13:00 AM");
    fbo.setEndTimePhrase("3:22 PM");
    fbo.setStartDatePhrase("1/1/2009");
    fbo.setEndDatePhrase("6/1/2009");
    fbo.setVisitorsPerAppointment(1);

    Errors errors = new BindException(fbo, "command");

    BlockBuilderFormBackingObjectValidator validator = new BlockBuilderFormBackingObjectValidator();
    validator.validate(fbo, errors);
    List errorCodes = errors.getAllErrors();
    Assert.assertEquals(1, errorCodes.size());
    FieldError startTimeError = errors.getFieldError("startTimePhrase");
    Assert.assertNotNull(startTimeError);
    Assert.assertEquals("field.militarytime", startTimeError.getCode());

    fbo.setStartTimePhrase("abcde");
    errors = new BindException(fbo, "command");
    validator.validate(fbo, errors);
    Assert.assertEquals(1, errorCodes.size());
    FieldError startTimeError2 = errors.getFieldError("startTimePhrase");
    Assert.assertNotNull(startTimeError2);
    Assert.assertEquals("field.timeparseexception", startTimeError2.getCode());

    // reset fields for testing AVAIL-117
    fbo = new BlockBuilderFormBackingObject();
    fbo.setDaysOfWeekPhrase("NMTWRFS");
    fbo.setStartTimePhrase("10:90 AM");
    fbo.setEndTimePhrase("3:00 PM");
    fbo.setStartDatePhrase("1/1/2009");
    fbo.setEndDatePhrase("6/1/2009");
    fbo.setVisitorsPerAppointment(1);

    errors = new BindException(fbo, "command");
    validator.validate(fbo, errors);
    errorCodes = errors.getAllErrors();
    Assert.assertEquals(1, errorCodes.size());
    startTimeError = errors.getFieldError("startTimePhrase");
    Assert.assertNotNull(startTimeError);
    Assert.assertEquals("field.timeparseexception", startTimeError.getCode());
}

From source file:eu.scidipes.toolkits.pawebapp.web.ItemsController.java

@RequestMapping(value = { "/edit", "/{formID}/edit", "/new" }, method = RequestMethod.POST)
public String saveItem(@ModelAttribute final SaveAction saveAction, @ModelAttribute final Form form,
        final BindingResult result, final SessionStatus status, final RedirectAttributes redirectAttrs,
        @RequestPart(value = "dataHolder", required = false) final MultipartFile dataFile,
        @MatrixVariable(value = "fn", required = false, pathVar = "datasetName") final String formName) {

    formValidator.validate(form, result);
    if (result.hasErrors()) {
        boolean fixErrors = true;

        final List<FieldError> dataHolderErrors = result.getFieldErrors("dataHolder");

        /*//from w w  w .j a  v a  2  s.  c  o m
         * Only ignore errors under very specific conditions, i.e. when there is a single no-file-supplied error and
         * a current file exists
         */
        if (result.getErrorCount() == 1 && dataHolderErrors != null && dataHolderErrors.size() == 1) {

            final FieldError dataHolderError = dataHolderErrors.get(0);
            if (FormValidator.FILE_REQ_ERR_CODE.equals(dataHolderError.getCode())
                    && !form.getDataHolderMetadata().isEmpty() && form.getDataHolderType() == BYTESTREAM) {
                fixErrors = false;
            }

        }

        if (fixErrors) {
            return "datasets/items/edit";
        }
    }

    /* Ensure that the dataHolder field is not overwritten when its an empty upload (i.e. re-save) */
    if (form.getDataHolderType() == BYTESTREAM && dataFile != null && !dataFile.isEmpty()) {
        LOG.debug("incoming dataFile: {}", dataFile);
        form.getDataHolderMetadata().put(FILE_NAME, dataFile.getOriginalFilename());
        form.getDataHolderMetadata().put(FILE_SIZE, String.valueOf(dataFile.getSize()));
        form.getDataHolderMetadata().put(FILE_MIMETYPE, dataFile.getContentType());
    } else if (form.getDataHolderType() == URI && !StringUtils.isEmpty(form.getDataHolder())) {
        LOG.debug("incoming dataHolder: {}", form.getDataHolder());
        form.getDataHolderMetadata().clear();
    }

    final FormsBundleImpl dataset = datasetRepo.findOne(form.getParentBundle().getDatasetName());

    if (saveAction == ADD_NEW) {
        dataset.addForm(form);
    } else {
        dataset.setForm(form);
    }

    final FormsBundle savedDataset = datasetRepo.save(dataset);
    final Form savedForm = savedDataset.getForms().get(savedDataset.getForms().indexOf(form));

    status.setComplete();
    redirectAttrs.addFlashAttribute("msgKey", "items.edit.messages.savesuccess");

    final String formNameMatrixVar = StringUtils.isEmpty(formName) ? "" : ";fn=" + formName;
    return "redirect:/datasets/" + savedDataset.getDatasetName() + formNameMatrixVar + "/items/"
            + savedForm.getFormID() + "/edit";
}

From source file:com.citrix.cpbm.portal.fragment.controllers.AbstractReportController.java

@RequestMapping(value = { "", "/new_registrations" }, method = { RequestMethod.POST })
public String newRegistrations(ModelMap modelMap, HttpServletRequest request,
        @ModelAttribute(value = "registrationFormReport") NewRegistrationReportForm newRegistrationForm,
        BindingResult result) {//from w  w w .j  a v a  2  s.com
    logger.debug("###Entering in NewRegistrations(modelMap) method @POST");
    setPage(modelMap, Page.REPORTS_HOME);
    NewRegistrationReportValidator newRegistrationValidator = new NewRegistrationReportValidator();
    newRegistrationValidator.validate(newRegistrationForm, result);
    if (result.hasErrors()) {
        modelMap.addAttribute("registrationFormReport", newRegistrationForm);
        List<FieldError> l = result.getFieldErrors();
        for (FieldError f : l) {
            if (f.getCode().equals("js.report.errors.validationDate")) {
                return "report.newRegistration";
            }
        }
    }
    getRegistrationReport(newRegistrationForm.getStartDate(), newRegistrationForm.getEndDate(), modelMap,
            getSessionLocale(request));
    logger.debug("###Exiting NewRegistrations(modelMap) method @POST");
    return "report.newRegistration";
}

From source file:com.citrix.cpbm.portal.fragment.controllers.AbstractReportController.java

@RequestMapping(value = "/customer_rank", method = { RequestMethod.POST })
public String customersRank(ModelMap modelMap, HttpServletRequest request,
        @ModelAttribute(value = "customerRankReport") CustomerRankReportForm customerRankReportForm,
        BindingResult result) {//from www  . j a  v  a2 s.co  m
    logger.debug("###Entering in customerRank(modelMap) method @POST");
    CustomerRankReportValidator customerRankReportValidator = new CustomerRankReportValidator();
    customerRankReportValidator.validate(customerRankReportForm, result);
    if (result.hasErrors()) {
        setPage(modelMap, Page.REPORTS_CUSTOMER_RANK);
        modelMap.addAttribute("customerRankReport", customerRankReportForm);
        List<FieldError> l = result.getFieldErrors();
        for (FieldError f : l) {
            if (f.getCode().equals("js.report.errors.validationMonth")
                    || f.getCode().equals("js.report.errors.validationYear")) {
                addMonthAndYearToMap(modelMap, getSessionLocale(request));
                return "report.customerRank";
            }
        }
    }
    String status = getReport(customerRankReportForm.getReportMonth(), customerRankReportForm.getReportYear(),
            modelMap, CustomerRankReport.class, Page.REPORTS_CUSTOMER_RANK, getSessionLocale(request));
    if (status.equalsIgnoreCase("failure")) {
        return "failure";
    }
    modelMap.addAttribute("customerRankReport", customerRankReportForm);
    modelMap.addAttribute("rotateYAxisLabels", true);
    modelMap.addAttribute("formatNumber", true);
    logger.debug("###Exiting customerRank(modelMap) method @POST");
    return "report.customerRank";
}

From source file:com.citrix.cpbm.portal.fragment.controllers.AbstractReportController.java

@RequestMapping(value = "/product_usage", method = { RequestMethod.POST })
public String productUsage(ModelMap modelMap, HttpServletRequest request,
        @ModelAttribute(value = "customerRankReport") CustomerRankReportForm customerRankReportForm,
        BindingResult result) {//from   w ww  .j a  va 2 s . com
    logger.debug("###Entering in productUsage(modelMap) method @POST");
    CustomerRankReportValidator customerRankReportValidator = new CustomerRankReportValidator();
    customerRankReportValidator.validate(customerRankReportForm, result);
    if (result.hasErrors()) {
        setPage(modelMap, Page.REPORTS_PRODUCT_USAGE);
        modelMap.addAttribute("customerRankReport", customerRankReportForm);
        List<FieldError> l = result.getFieldErrors();
        for (FieldError f : l) {
            if (f.getCode().equals("js.report.errors.validationMonth")
                    || f.getCode().equals("js.report.errors.validationYear")) {
                addMonthAndYearToMap(modelMap, getSessionLocale(request));
                return "report.usageByProduct";
            }
        }
    }
    String status = getReport(customerRankReportForm.getReportMonth(), customerRankReportForm.getReportYear(),
            modelMap, MonthlyProductUsage.class, Page.REPORTS_PRODUCT_USAGE, getSessionLocale(request));
    if (status.equalsIgnoreCase("failure")) {
        return "failure";
    }
    modelMap.addAttribute("customerRankReport", customerRankReportForm);
    modelMap.addAttribute("usageReportName", "productUsage");
    modelMap.addAttribute("rotateXAxisLabels", true);
    logger.debug("###Exiting productUsage(modelMap) method @POST");
    return "report.usageByProduct";
}

From source file:com.citrix.cpbm.portal.fragment.controllers.AbstractReportController.java

@RequestMapping(value = "/productbundle_usage", method = { RequestMethod.POST })
public String productBundleUsage(ModelMap modelMap, HttpServletRequest request,
        @ModelAttribute(value = "customerRankReport") CustomerRankReportForm customerRankReportForm,
        BindingResult result) {/* w  w  w.j a  v a2  s  .c o  m*/
    logger.debug("###Entering in productBundleUsage(modelMap) method @POST");
    CustomerRankReportValidator customerRankReportValidator = new CustomerRankReportValidator();
    customerRankReportValidator.validate(customerRankReportForm, result);
    if (result.hasErrors()) {
        setPage(modelMap, Page.REPORTS_PRODUCT_BUNDLE_USAGE);
        modelMap.addAttribute("customerRankReport", customerRankReportForm);
        List<FieldError> l = result.getFieldErrors();
        for (FieldError f : l) {
            if (f.getCode().equals("js.report.errors.validationMonth")
                    || f.getCode().equals("js.report.errors.validationYear")) {
                return "report.usageByProductBundle";
            }
        }
    }
    String status = getReport(customerRankReportForm.getReportMonth(), customerRankReportForm.getReportYear(),
            modelMap, MonthlyProductBundleUsage.class, Page.REPORTS_PRODUCT_BUNDLE_USAGE,
            getSessionLocale(request));
    if (status.equalsIgnoreCase("failure")) {
        return "failure";
    }
    modelMap.addAttribute("customerRankReport", customerRankReportForm);
    modelMap.addAttribute("usageReportName", "productbundleUsage");
    modelMap.addAttribute("rotateXAxisLabels", true);
    logger.debug("###Exiting productBundleUsage(modelMap) method @POST");
    return "report.usageByProductBundle";
}

From source file:ch.ralscha.extdirectspring.bean.ExtDirectResponseBuilder.java

/**
 * Adds an "errors" property in the response if there are any errors in the
 * bindingResult. Sets the success flag to false if there are errors.
 *
 * @param locale/*from   ww w .  j  a  v  a  2 s  .c  om*/
 * @param messageSource
 * @param bindingResult
 * @return this instance
 */
public ExtDirectResponseBuilder addErrors(Locale locale, MessageSource messageSource,
        final BindingResult bindingResult) {
    if (bindingResult != null && bindingResult.hasFieldErrors()) {
        Map<String, List<String>> errorMap = new HashMap<String, List<String>>();
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            String message = fieldError.getDefaultMessage();
            if (messageSource != null) {
                Locale loc = locale != null ? locale : Locale.getDefault();
                message = messageSource.getMessage(fieldError.getCode(), fieldError.getArguments(), loc);
            }
            List<String> fieldErrors = errorMap.get(fieldError.getField());

            if (fieldErrors == null) {
                fieldErrors = new ArrayList<String>();
                errorMap.put(fieldError.getField(), fieldErrors);
            }

            fieldErrors.add(message);
        }
        if (errorMap.isEmpty()) {
            addResultProperty("success", Boolean.TRUE);
        } else {
            addResultProperty("errors", errorMap);
            addResultProperty("success", Boolean.FALSE);
        }
    }
    return this;
}