Java Code Examples for org.springframework.validation.BindException#reject()

The following examples show how to use org.springframework.validation.BindException#reject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PrintingResultHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void modelAndView() throws Exception {
	BindException bindException = new BindException(new Object(), "target");
	bindException.reject("errorCode");

	ModelAndView mav = new ModelAndView("viewName");
	mav.addObject("attrName", "attrValue");
	mav.addObject(BindingResult.MODEL_KEY_PREFIX + "attrName", bindException);

	this.mvcResult.setMav(mav);
	this.handler.handle(this.mvcResult);

	assertValue("ModelAndView", "View name", "viewName");
	assertValue("ModelAndView", "View", null);
	assertValue("ModelAndView", "Attribute", "attrName");
	assertValue("ModelAndView", "value", "attrValue");
	assertValue("ModelAndView", "errors", bindException.getAllErrors());
}
 
Example 2
Source File: PrintingResultHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void modelAndView() throws Exception {
	BindException bindException = new BindException(new Object(), "target");
	bindException.reject("errorCode");

	ModelAndView mav = new ModelAndView("viewName");
	mav.addObject("attrName", "attrValue");
	mav.addObject(BindingResult.MODEL_KEY_PREFIX + "attrName", bindException);

	this.mvcResult.setMav(mav);
	this.handler.handle(this.mvcResult);

	assertValue("ModelAndView", "View name", "viewName");
	assertValue("ModelAndView", "View", null);
	assertValue("ModelAndView", "Attribute", "attrName");
	assertValue("ModelAndView", "value", "attrValue");
	assertValue("ModelAndView", "errors", bindException.getAllErrors());
}
 
Example 3
Source File: PrintingResultHandlerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void modelAndView() throws Exception {
	BindException bindException = new BindException(new Object(), "target");
	bindException.reject("errorCode");

	ModelAndView mav = new ModelAndView("viewName");
	mav.addObject("attrName", "attrValue");
	mav.addObject(BindingResult.MODEL_KEY_PREFIX + "attrName", bindException);

	this.mvcResult.setMav(mav);
	this.handler.handle(this.mvcResult);

	assertValue("ModelAndView", "View name", "viewName");
	assertValue("ModelAndView", "View", null);
	assertValue("ModelAndView", "Attribute", "attrName");
	assertValue("ModelAndView", "value", "attrValue");
	assertValue("ModelAndView", "errors", bindException.getAllErrors());
}
 
Example 4
Source File: InTrayController.java    From webcurator with Apache License 2.0 6 votes vote down vote up
private ModelAndView deleteTask(InTrayCommand intrayCmd, int pageSize, BindException aErrors, Boolean showTasks) {
     Long taskOid = intrayCmd.getTaskOid();
     if (taskOid != null) {
         try {
	inTrayManager.deleteTask(taskOid);
} 
         catch (NotOwnerRuntimeException e) {
         	aErrors.reject("task.error.delete.not.owner");
}
     } else {
         log.warn("A form was posted to the InTrayController without a valid taskOid attribute, redirecting to the showForm flow.");
     }
     
     ModelAndView mav = defaultView(intrayCmd.getTaskPage(), intrayCmd.getNotificationPage(), pageSize, showTasks);
     if (aErrors.hasErrors()) {
     	mav.addObject(Constants.GBL_ERRORS, aErrors);
     }
     
     return mav;
 }
 
Example 5
Source File: AccountFormController.java    From jpetstore-kubernetes with Apache License 2.0 5 votes vote down vote up
protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
	throws Exception {

AccountForm accountForm = (AccountForm) command;
Account account = accountForm.getAccount();

if (request.getParameter("account.listOption") == null) {
	account.setListOption(false);
}
if (request.getParameter("account.bannerOption") == null) {
	account.setBannerOption(false);
}

errors.setNestedPath("account");
getValidator().validate(account, errors);
errors.setNestedPath("");

if (accountForm.isNewAccount()) {
	account.setStatus("OK");
	ValidationUtils.rejectIfEmpty(errors, "account.username", "USER_ID_REQUIRED", "User ID is required.");
	if (account.getPassword() == null || account.getPassword().length() < 1 ||
			!account.getPassword().equals(accountForm.getRepeatedPassword())) {
	 errors.reject("PASSWORD_MISMATCH",
			 "Passwords did not match or were not provided. Matching passwords are required.");
	}
}
else if (account.getPassword() != null && account.getPassword().length() > 0) {
  if (!account.getPassword().equals(accountForm.getRepeatedPassword())) {
		errors.reject("PASSWORD_MISMATCH",
				"Passwords did not match. Matching passwords are required.");
  }
 }
}
 
Example 6
Source File: AccountFormController.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors)
	throws Exception {

AccountForm accountForm = (AccountForm) command;
Account account = accountForm.getAccount();

if (request.getParameter("account.listOption") == null) {
	account.setListOption(false);
}
if (request.getParameter("account.bannerOption") == null) {
	account.setBannerOption(false);
}

errors.setNestedPath("account");
getValidator().validate(account, errors);
errors.setNestedPath("");

if (accountForm.isNewAccount()) {
	account.setStatus("OK");
	ValidationUtils.rejectIfEmpty(errors, "account.username", "USER_ID_REQUIRED", "User ID is required.");
	if (account.getPassword() == null || account.getPassword().length() < 1 ||
			!account.getPassword().equals(accountForm.getRepeatedPassword())) {
	 errors.reject("PASSWORD_MISMATCH",
			 "Passwords did not match or were not provided. Matching passwords are required.");
	}
}
else if (account.getPassword() != null && account.getPassword().length() > 0) {
  if (!account.getPassword().equals(accountForm.getRepeatedPassword())) {
		errors.reject("PASSWORD_MISMATCH",
				"Passwords did not match. Matching passwords are required.");
  }
 }
}
 
Example 7
Source File: SiteController.java    From webcurator with Apache License 2.0 5 votes vote down vote up
private void checkAuthAgencyNamesUnique(HttpServletRequest request, BindException errors) {
	SiteEditorContext ctx = getEditorContext(request);
	for(AuthorisingAgent agent: ctx.getSite().getAuthorisingAgents()) {
		if( !siteManager.isAuthAgencyNameUnique( agent.isNew() ? null : agent.getOid(), agent.getName())) {
			errors.reject("site.errors.authagent.duplicatename", new Object[] { agent.getName() }, "");
		}
	}
}
 
Example 8
Source File: SiteController.java    From webcurator with Apache License 2.0 5 votes vote down vote up
public void checkSave(HttpServletRequest req, BindException errors) { 
	if (!errors.hasErrors() && !siteManager.isSiteTitleUnique(getEditorContext(req).getSite())) {
		errors.reject("site.errors.duplicatename", new Object[] {getEditorContext(req).getSite().getTitle()} ,"");
	}
	
	checkAuthAgencyNamesUnique(req, errors);
}
 
Example 9
Source File: MakeDefaultProfileController.java    From webcurator with Apache License 2.0 5 votes vote down vote up
@Override
protected ModelAndView handle(HttpServletRequest req, HttpServletResponse res, Object comm, BindException errors) throws Exception {
	ViewCommand command = (ViewCommand) comm;
	Profile profile = profileManager.load(command.getProfileOid());
	if(authorityManager.hasPrivilege(profile, Privilege.MANAGE_PROFILES)) {
		boolean showInactive = (Boolean) req.getSession().getAttribute(ProfileListController.SESSION_KEY_SHOW_INACTIVE);
        String defaultAgency = (String)req.getSession().getAttribute(ProfileListController.SESSION_AGENCY_FILTER);
        if(defaultAgency == null)
        {
        	defaultAgency = AuthUtil.getRemoteUserObject().getAgency().getName();
        }
		ProfileListCommand pcomm = new ProfileListCommand();
		pcomm.setShowInactive(showInactive);
        pcomm.setDefaultAgency(defaultAgency);
		ModelAndView mav = null;
		try {
			profileManager.setProfileAsDefault(profile);
			mav = getView(pcomm);
		} 
		catch (WCTInvalidStateRuntimeException e) {
			Object[] vals = new Object[] {profile.getName(), profile.getOwningAgency().getName()};
			errors.reject("profile.inactive", vals, "The profile is inactive");			
			mav = getView(pcomm);
			mav.addObject(Constants.GBL_ERRORS, errors);
		}
		
		return mav;
	}
	else {
		return CommonViews.AUTHORISATION_FAILURE;
	}
}
 
Example 10
Source File: BandwidthRestrictionsController.java    From webcurator with Apache License 2.0 4 votes vote down vote up
private ModelAndView processSaveHeatmap(HttpServletRequest aReq, HttpServletResponse aResp,
		BandwidthRestrictionsCommand command, BindException aErrors) {

	HeatmapConfigDTO lowConfig = command.getLowHeatmapConfig();
	HeatmapConfigDTO mediumConfig = command.getMediumHeatmapConfig();
	HeatmapConfigDTO highConfig = command.getHighHeatmapConfig();

	ModelAndView mav = createDefaultModelAndView();
	if (aErrors.hasErrors()) {
		mav.addObject(Constants.GBL_CMD_DATA, command);
		mav.addObject(Constants.GBL_ERRORS, aErrors);
		return mav;
	}
	if (lowConfig.getThresholdLowest() <= 0) {
		aErrors.reject("heatmap.errors.gtzero");
		mav.addObject(Constants.GBL_CMD_DATA, command);
		mav.addObject(Constants.GBL_ERRORS, aErrors);
		return mav;
	}
	if (lowConfig.getThresholdLowest() >= mediumConfig.getThresholdLowest()) {
		aErrors.reject("heatmap.errors.lowGtMedium");
		mav.addObject(Constants.GBL_CMD_DATA, command);
		mav.addObject(Constants.GBL_ERRORS, aErrors);
		return mav;
	}
	if (mediumConfig.getThresholdLowest() >= highConfig.getThresholdLowest()) {
		aErrors.reject("Threshold for medium heatmap color must be lower than high");
		mav.addObject(Constants.GBL_CMD_DATA, command);
		mav.addObject(Constants.GBL_ERRORS, aErrors);
		return mav;
	}

	// Verify low is lowest, etc
	saveHeatmapConfig(lowConfig);
	saveHeatmapConfig(mediumConfig);
	saveHeatmapConfig(highConfig);

	mav.addObject(Constants.GBL_MESSAGES,
			messageSource.getMessage("heatmap.config.saved", new Object[] {}, Locale.getDefault()));
	return mav;
}