You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BeanValidationBinder.java 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data;
  17. import com.vaadin.data.util.BeanUtil;
  18. import com.vaadin.data.validator.BeanValidator;
  19. /**
  20. * @author Vaadin Ltd
  21. * @see Binder
  22. * @see HasValue
  23. *
  24. * @since 8.0
  25. */
  26. public class BeanValidationBinder<BEAN> extends Binder<BEAN> {
  27. private final Class<BEAN> beanType;
  28. /**
  29. * Creates a new binder that uses reflection based on the provided bean type
  30. * to resolve bean properties. It assumes that JSR-303 bean validation
  31. * implementation is present on the classpath. If there is no such
  32. * implementation available then {@link Binder} class should be used instead
  33. * (this constructor will throw an exception). Otherwise
  34. * {@link BeanValidator} is added to each binding that is defined using a
  35. * property name.
  36. *
  37. * @param beanType
  38. * the bean type to use, not <code>null</code>
  39. */
  40. public BeanValidationBinder(Class<BEAN> beanType) {
  41. super(beanType);
  42. if (!BeanUtil.checkBeanValidationAvailable()) {
  43. throw new IllegalStateException(
  44. BeanValidationBinder.class.getSimpleName()
  45. + " cannot be used because a JSR-303 Bean Validation "
  46. + "implementation not found on the classpath. Use "
  47. + Binder.class.getSimpleName() + " instead");
  48. }
  49. this.beanType = beanType;
  50. }
  51. @Override
  52. protected BindingBuilder<BEAN, ?> configureBinding(
  53. BindingBuilder<BEAN, ?> binding,
  54. PropertyDefinition<BEAN, ?> definition) {
  55. return binding.withValidator(
  56. new BeanValidator(beanType, definition.getName()));
  57. }
  58. }