List of usage examples for org.springframework.validation ValidationUtils rejectIfEmptyOrWhitespace
public static void rejectIfEmptyOrWhitespace(Errors errors, String field, String errorCode, @Nullable Object[] errorArgs)
From source file:org.openmrs.validator.SchedulerFormValidator.java
/** * Checks the form object for any inconsistencies/errors * //from w ww . j a v a 2 s . c om * @see org.springframework.validation.Validator#validate(java.lang.Object, * org.springframework.validation.Errors) * @should fail validation if name is null or empty or whitespace * @should fail validation if taskClass is empty or whitespace * @should fail validation if repeatInterval is null or empty or whitespace * @should fail validation if class is not instance of Task * @should fail validation if class is not accessible * @should fail validation if class cannot be instantiated * @should fail validation if class not found * @should pass validation if all required fields have proper values * @should pass validation if field lengths are correct * @should fail validation if field lengths are not correct */ public void validate(Object obj, Errors errors) { TaskDefinition taskDefinition = (TaskDefinition) obj; if (taskDefinition == null) { errors.rejectValue("task", "error.general"); } else { //Won't work without name and description properties on Task Definition ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "Scheduler.taskForm.required", new Object[] { "Task name", taskDefinition.getName() }); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "taskClass", "Scheduler.taskForm.required", new Object[] { "Task class", taskDefinition.getTaskClass() }); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "repeatInterval", "Scheduler.taskForm.required", new Object[] { "Repeat interval", taskDefinition.getRepeatInterval() }); ValidateUtil.validateFieldLengths(errors, obj.getClass(), "name", "description", "taskClass", "startTimePattern"); // Check if the class is valid try { Class<?> taskClass = OpenmrsClassLoader.getInstance().loadClass(taskDefinition.getTaskClass()); Object o = taskClass.newInstance(); if (!(o instanceof Task)) { errors.rejectValue("taskClass", "Scheduler.taskForm.classDoesNotImplementTask", new Object[] { taskDefinition.getTaskClass(), Task.class.getName() }, "Class does not implement Task interface"); } } catch (IllegalAccessException iae) { errors.rejectValue("taskClass", "Scheduler.taskForm.illegalAccessException", new Object[] { taskDefinition.getTaskClass() }, "Illegal access exception."); } catch (InstantiationException ie) { errors.rejectValue("taskClass", "Scheduler.taskForm.instantiationException", new Object[] { taskDefinition.getTaskClass() }, "Error creating new instance of class."); } catch (ClassNotFoundException cnfe) { errors.rejectValue("taskClass", "Scheduler.taskForm.classNotFoundException", new Object[] { taskDefinition.getTaskClass() }, "Class not found error."); } } }
From source file:ru.org.linux.spring.AddRemoveBoxesController.java
@RequestMapping(value = "/remove-box.jsp", method = RequestMethod.POST) public String doRemove(@ModelAttribute("form") EditBoxesForm form, BindingResult result, SessionStatus status, HttpServletRequest request) throws Exception { Template t = Template.getTemplate(request); if (!t.isSessionAuthorized()) { throw new AccessViolationException("Not authorized"); }//from w w w . j a v a 2s . c om new EditBoxesFormValidator().validate(form, result); ValidationUtils.rejectIfEmptyOrWhitespace(result, "position", "position.empty", "? a ? ?"); if (result.hasErrors()) { return "remove-box"; } if (result.hasErrors()) { return "remove-box"; } String objectName = getObjectName(form, request); List<String> boxlets = new ArrayList<String>(t.getProf().getList(objectName)); if (!boxlets.isEmpty()) { if (boxlets.size() > form.position) { boxlets.remove(form.position.intValue()); t.getProf().setList(objectName, boxlets); t.writeProfile(t.getProfileName()); } } status.setComplete(); return "redirect:/edit-boxes.jsp"; }
From source file:ru.org.linux.spring.AddRemoveBoxesController.java
@RequestMapping(value = "/add-box.jsp", method = RequestMethod.POST) public String doAdd(@ModelAttribute("form") EditBoxesForm form, BindingResult result, SessionStatus status, HttpServletRequest request)/* w w w. j a v a 2s .c om*/ throws IOException, UtilException, AccessViolationException, StorageException { new EditBoxesFormValidator().validate(form, result); ValidationUtils.rejectIfEmptyOrWhitespace(result, "boxName", "boxName.empty", "? ?"); if (StringUtils.isNotEmpty(form.getBoxName()) && !DefaultProfile.isBox(form.getBoxName())) { result.addError(new FieldError("boxName", "boxName.invalid", "? ?")); } if (result.hasErrors()) { return "add-box"; } Template t = Template.getTemplate(request); if (result.hasErrors()) { return "add-box"; } if (form.getPosition() == null) { form.setPosition(0); } String objectName = getObjectName(form, request); List<String> boxlets = new ArrayList<String>(t.getProf().getList(objectName)); CollectionUtils.filter(boxlets, DefaultProfile.getBoxPredicate()); if (boxlets.size() > form.position) { boxlets.add(form.position, form.boxName); } else { boxlets.add(form.boxName); } t.getProf().setList(objectName, boxlets); t.writeProfile(t.getProfileName()); status.setComplete(); return "redirect:/edit-boxes.jsp"; }
From source file:ru.org.linux.user.AddRemoveBoxesController.java
@RequestMapping(value = "/remove-box.jsp", method = RequestMethod.POST) public String doRemove(@ModelAttribute("form") EditBoxesRequest form, BindingResult result, SessionStatus status, HttpServletRequest request) throws Exception { Template t = Template.getTemplate(request); if (!t.isSessionAuthorized()) { throw new AccessViolationException("Not authorized"); }//www. j a v a2s . co m new EditBoxesRequestValidator().validate(form, result); ValidationUtils.rejectIfEmptyOrWhitespace(result, "position", "position.empty", "? a ? ?"); if (result.hasErrors()) { return "remove-box"; } if (result.hasErrors()) { return "remove-box"; } String objectName = getObjectName(form, request); List<String> boxlets = new ArrayList<String>(t.getProf().getList(objectName)); if (!boxlets.isEmpty()) { if (boxlets.size() > form.position) { boxlets.remove(form.position.intValue()); t.getProf().setList(objectName, boxlets); t.writeProfile(t.getProfileName()); } } status.setComplete(); return "redirect:/edit-boxes.jsp"; }
From source file:ru.org.linux.user.AddRemoveBoxesController.java
@RequestMapping(value = "/add-box.jsp", method = RequestMethod.POST) public String doAdd(@ModelAttribute("form") EditBoxesRequest form, BindingResult result, SessionStatus status, HttpServletRequest request) throws IOException, AccessViolationException, StorageException { new EditBoxesRequestValidator().validate(form, result); ValidationUtils.rejectIfEmptyOrWhitespace(result, "boxName", "boxName.empty", "? ?"); if (StringUtils.isNotEmpty(form.getBoxName()) && !DefaultProfile.isBox(form.getBoxName())) { result.addError(new FieldError("boxName", "boxName.invalid", "? ?")); }/*from w w w. j a va 2s. c o m*/ if (result.hasErrors()) { return "add-box"; } Template t = Template.getTemplate(request); if (result.hasErrors()) { return "add-box"; } if (form.getPosition() == null) { form.setPosition(0); } String objectName = getObjectName(form, request); List<String> boxlets = new ArrayList<String>(t.getProf().getList(objectName)); CollectionUtils.filter(boxlets, DefaultProfile.getBoxPredicate()); if (boxlets.size() > form.position) { boxlets.add(form.position, form.boxName); } else { boxlets.add(form.boxName); } t.getProf().setList(objectName, boxlets); t.writeProfile(t.getProfileName()); status.setComplete(); return "redirect:/edit-boxes.jsp"; }