List of usage examples for org.springframework.web.bind WebDataBinder validate
public void validate()
From source file:cherry.foundation.validator.MinLengthValidatorTest.java
@Test public void testNG() { Map<String, String> val = new HashMap<>(); val.put("val3", "1234"); val.put("val4", "123"); TestDto dto = new TestDto(); WebDataBinder binder = new WebDataBinder(dto); binder.setConversionService(conversionService); binder.addValidators(validator);// w w w.ja v a 2 s . c o m binder.bind(new MutablePropertyValues(val)); binder.validate(); BindingResult result = binder.getBindingResult(); assertEquals(2, result.getErrorCount()); }
From source file:cherry.foundation.validator.MinLengthValidatorTest.java
@Test public void testOK() { Map<String, String> val = new HashMap<>(); val.put("val0", null); val.put("val1", ""); val.put("val2", "1"); val.put("val3", "12345"); val.put("val4", "123456"); TestDto dto = new TestDto(); WebDataBinder binder = new WebDataBinder(dto); binder.setConversionService(conversionService); binder.addValidators(validator);//from w w w. jav a 2 s .co m binder.bind(new MutablePropertyValues(val)); binder.validate(); BindingResult result = binder.getBindingResult(); assertEquals(0, result.getErrorCount()); assertNull(dto.getVal0()); assertEquals("", dto.getVal1()); assertEquals("1", dto.getVal2()); assertEquals("12345", dto.getVal3()); assertEquals("123456", dto.getVal4()); }
From source file:cherry.foundation.validator.ZipCodeValidatorTest.java
@Test public void testNG() { Map<String, String> val = new HashMap<>(); val.put("zipCode0", "123456"); val.put("zipCode1", "12345678"); val.put("zipCode2", "abcdefg"); val.put("zipCode3", "123-456"); val.put("zipCode4", "123-45678"); val.put("zipCode5", "abc-defg"); TestDto dto = new TestDto(); WebDataBinder binder = new WebDataBinder(dto); binder.setConversionService(conversionService); binder.addValidators(validator);//from w w w .j a v a 2s . c o m binder.bind(new MutablePropertyValues(val)); binder.validate(); BindingResult result = binder.getBindingResult(); assertEquals(6, result.getErrorCount()); }
From source file:cherry.foundation.validator.ZipCodeValidatorTest.java
@Test public void testOK() { Map<String, String> val = new HashMap<>(); val.put("zipCode0", null); val.put("zipCode1", ""); val.put("zipCode2", "1234567"); val.put("zipCode3", null); val.put("zipCode4", ""); val.put("zipCode5", "123-4567"); TestDto dto = new TestDto(); WebDataBinder binder = new WebDataBinder(dto); binder.setConversionService(conversionService); binder.addValidators(validator);//from w ww . ja va 2 s . c om binder.bind(new MutablePropertyValues(val)); binder.validate(); BindingResult result = binder.getBindingResult(); assertEquals(0, result.getErrorCount()); assertNull(dto.getZipCode0()); assertEquals("", dto.getZipCode1()); assertEquals("1234567", dto.getZipCode2()); assertNull(dto.getZipCode3()); assertEquals("", dto.getZipCode4()); assertEquals("123-4567", dto.getZipCode5()); }
From source file:cherry.foundation.validator.TelNoValidatorTest.java
@Test public void testNG() { Map<String, String> val = new HashMap<>(); val.put("telNo0", "01-234-567"); val.put("telNo1", "0123-1234-5678"); val.put("telNo2", "0a-2345-6789"); val.put("telNo3", "01-a345-6789"); val.put("telNo4", "01-234a-6789"); val.put("telNo5", "01-2345-a789"); val.put("telNo6", "01-2345-678a"); TestDto dto = new TestDto(); WebDataBinder binder = new WebDataBinder(dto); binder.setConversionService(conversionService); binder.addValidators(validator);/*from w w w. ja v a 2 s .c om*/ binder.bind(new MutablePropertyValues(val)); binder.validate(); BindingResult result = binder.getBindingResult(); assertEquals(7, result.getErrorCount()); }
From source file:cherry.foundation.validator.CharTypeValidatorTest.java
@Test public void testNG() { Map<String, String> val = new HashMap<>(); val.put("none", "0"); val.put("space", " \t\r\n0"); val.put("numeric", "0123456789A"); val.put("alpha", "ABCabc0"); val.put("upper", "ABCa"); val.put("lower", "abcA"); TestDto dto = new TestDto(); WebDataBinder binder = new WebDataBinder(dto); binder.setConversionService(conversionService); binder.addValidators(validator);/* www. ja v a2s .co m*/ binder.bind(new MutablePropertyValues(val)); binder.validate(); BindingResult result = binder.getBindingResult(); assertThat(result.getErrorCount(), is(6)); }
From source file:cherry.foundation.validator.TelNoValidatorTest.java
@Test public void testOK() { Map<String, String> val = new HashMap<>(); val.put("telNo0", null); val.put("telNo1", ""); val.put("telNo2", "01-234-5678"); val.put("telNo3", "01-2345-6789"); val.put("telNo4", "012-345-6789"); val.put("telNo5", "0123-45-6789"); val.put("telNo6", "01234-5-6789"); val.put("telNo7", "090-1234-5678"); val.put("telNo8", "0120-123-456"); TestDto dto = new TestDto(); WebDataBinder binder = new WebDataBinder(dto); binder.setConversionService(conversionService); binder.addValidators(validator);/* w ww. j a v a 2 s . c o m*/ binder.bind(new MutablePropertyValues(val)); binder.validate(); BindingResult result = binder.getBindingResult(); assertEquals(0, result.getErrorCount()); assertNull(dto.getTelNo0()); assertEquals("", dto.getTelNo1()); assertEquals("01-234-5678", dto.getTelNo2()); assertEquals("01-2345-6789", dto.getTelNo3()); assertEquals("012-345-6789", dto.getTelNo4()); assertEquals("0123-45-6789", dto.getTelNo5()); assertEquals("01234-5-6789", dto.getTelNo6()); assertEquals("090-1234-5678", dto.getTelNo7()); assertEquals("0120-123-456", dto.getTelNo8()); }
From source file:cherry.foundation.validator.CharTypeValidatorTest.java
@Test public void testOK() { Map<String, String> val = new HashMap<>(); val.put("none", ""); val.put("space", " \t\r\n"); val.put("numeric", "0123456789"); val.put("alpha", "ABCabc"); val.put("upper", "ABC"); val.put("lower", "abc"); val.put("surrogate", "\uD842\uDF9F"); TestDto dto = new TestDto(); WebDataBinder binder = new WebDataBinder(dto); binder.setConversionService(conversionService); binder.addValidators(validator);//from www. j a v a 2 s.c o m binder.bind(new MutablePropertyValues(val)); binder.validate(); BindingResult result = binder.getBindingResult(); assertThat(result.getErrorCount(), is(0)); assertThat(dto.getSpace(), is(" \t\r\n")); assertThat(dto.getNumeric(), is("0123456789")); assertThat(dto.getAlpha(), is("ABCabc")); assertThat(dto.getUpper(), is("ABC")); assertThat(dto.getLower(), is("abc")); assertThat(dto.getSurrogate(), is("\uD842\uDF9F")); }
From source file:org.opentides.web.processor.FormBindMethodProcessor.java
/** * Resolve the argument from the model or if not found instantiate it with * its default. The model attribute is then populated with request values * via data binding and validated. If no validation error, the model * is retrieved from database and merged with the submitted form. * /*from w ww . j a v a2s .co m*/ * @throws BindException if data binding and validation result in an error * @throws Exception if WebDataBinder initialization fails. */ @SuppressWarnings("unchecked") public final Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest nativeRequest, WebDataBinderFactory binderFactory) throws Exception { FormBind annot = parameter.getParameterAnnotation(FormBind.class); Class<?> clazz = parameter.getDeclaringClass(); String name = getName(annot, parameter); HttpServletRequest request = (HttpServletRequest) nativeRequest.getNativeRequest(); Method addForm = CacheUtil.getNewFormBindMethod(clazz); Object controller = beanFactory.getBean(parameter.getContainingClass()); Object target = (addForm != null) ? addForm.invoke(controller, request) : BeanUtils.instantiateClass(parameter.getParameterType()); MutablePropertyValues mpvs = new MutablePropertyValues(nativeRequest.getParameterMap()); WebDataBinder binder = binderFactory.createBinder(nativeRequest, target, name); if (binder.getTarget() != null) { binder.bind(mpvs); if (binder.getValidator() != null) binder.validate(); if (binder.getBindingResult().hasErrors()) { throw new BindException(binder.getBindingResult()); } String method = request.getMethod().toLowerCase(); // id should be the last segment of the uri String uri = request.getRequestURI(); String sid = uri.substring(uri.lastIndexOf("/") + 1); Long id = StringUtil.convertToLong(sid, 0); // if target extends BaseEntity and for update, link target to database record if (("put".equals(method) || "post".equals(method)) && id > 0 && BaseEntity.class.isAssignableFrom(parameter.getParameterType())) { // now retrieve record from database for updating Method updateForm = CacheUtil.getUpdateFormBindMethod(clazz); BaseEntity record = null; if (updateForm == null) { // no annotation, invoke from service Method getService = controller.getClass().getMethod("getService"); if (getService == null) { String message = "Cannot find method with @FormBind with update mode. " + "Also, unable to find service associated to controller." + "Please specify one that retrieves record from database."; throw new InvalidImplementationException(message); } BaseCrudService<? extends BaseEntity> service = (BaseCrudService<? extends BaseEntity>) getService .invoke(controller); record = (BaseEntity) service.load(sid); } else { // with annotation, invoke annotation record = (BaseEntity) updateForm.invoke(controller, sid, request); } if (record != null) { WebDataBinder updateBinder = binderFactory.createBinder(nativeRequest, record, name); updateBinder.bind(mpvs); mavContainer.addAllAttributes(updateBinder.getBindingResult().getModel()); return updateBinder.getTarget(); } else { String message = "Unable to find " + parameter.getParameterType().getSimpleName() + " with id=" + sid + " for update."; throw new DataAccessException(message); } } else if ("post".equals(method)) { mavContainer.addAllAttributes(binder.getBindingResult().getModel()); return binder.getTarget(); } else { throw new InvalidImplementationException( "@FormBind argument annotation can only be used on POST or PUT methods."); } } mavContainer.addAllAttributes(binder.getBindingResult().getModel()); return binder.getTarget(); }
From source file:org.springframework.data.document.web.bind.annotation.support.HandlerMethodInvoker.java
private void doBind(WebDataBinder binder, NativeWebRequest webRequest, boolean validate, boolean failOnErrors) throws Exception { doBind(binder, webRequest);//from www . ja v a2 s .c om if (validate) { binder.validate(); } if (failOnErrors && binder.getBindingResult().hasErrors()) { throw new BindException(binder.getBindingResult()); } }