Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

components-fields.asciidoc 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. ---
  2. title: Field Components
  3. order: 4
  4. layout: page
  5. ---
  6. [[components.fields]]
  7. = Field Components
  8. *_This section has not yet been updated for Vaadin Framework 8_*
  9. ((("[classname]#Field#", id="term.components.fields", range="startofrange")))
  10. _Fields_ are components that have a value that the user can change through the
  11. user interface. <<figure.components.fields>> illustrates the inheritance relationships
  12. and the important interfaces and base classes.
  13. [[figure.components.fields]]
  14. .Field components
  15. image::img/field-diagram-hi.png[width=80%, scaledwidth=100%]
  16. Field components are built upon the framework defined in the [classname]#HasValue#
  17. interface.
  18. [classname]#AbstractField# is the base class for all field components,
  19. except those components that allow the user to select a value.
  20. (see <<dummy/../../../framework/components/components-selection.asciidoc#components.selection,"Selection Components">>).
  21. In addition to the component features inherited from
  22. [classname]#AbstractComponent#, it implements the features defined in the
  23. [interfacename]#HasValue# and [classname]#Component.Focusable# interfaces.
  24. [[figure.components.fields.hasvalue]]
  25. .Field components having values
  26. image::img/field-interface-v8-hi.png[width=60%, scaledwidth=100%]
  27. The description of the [interfacename]#HasValue# interface and field components extending [classname]#AbstractField] is broken down in the following sections.
  28. [[components.fields.field]]
  29. == The [interfacename]#HasValue# Interface
  30. The [interfacename]#HasValue# interface marks a component that has a user editable value.
  31. The type parameter in the interface is the type of the value that the component is editing.
  32. You can set the value with the [methodname]#setValue()# and read it with the
  33. [methodname]#getValue()# method defined in the [classname]#HasValue# interface.
  34. The [classname]#HasValue# interface defines a number of properties, which you can
  35. access with the corresponding setters and getters.
  36. [methodname]#readOnly#:: Set the component to be read-only, meaning that the value is not editable.
  37. [methodname]#requiredIndicatorVisible#:: When enabled, a required indicator
  38. (the asterisk * character) is displayed on the left, above, or right the field,
  39. depending on the containing layout and whether the field has a caption.
  40. When the component is used in a form (see <<dummy/../../../framework/datamodel/datamodel-forms.asciidoc#datamodel.forms.validation,"Validation">>),
  41. it can be set to be required, which will automatically show the required indicator,
  42. and validate that the value is not empty. Without validation, the required indicator
  43. is merely a visual guide.
  44. [methodname]#emptyValue#:: The initial empty value of the component.
  45. [methodname]#clear#:: Clears the value to the empty value.
  46. [[components.fields.valuechanges]]
  47. == Handling Value Changes
  48. [interfacename]#HasValue# provides [methodname]#addValueChangeListener# method for listening to changes to the field value. This method returns a [classname]#Registration# object that can be used to later
  49. remove the added listener if necessary.
  50. [source, java]
  51. ----
  52. TextField textField = new TextField();
  53. Label echo = new Label();
  54. textField.addValueChangeListener(event -> {
  55. String origin = event.isUserOriginated()
  56. ? "user"
  57. : "application";
  58. String message = origin
  59. + " entered the following: "
  60. + event.getValue();
  61. Notification.show(message);
  62. });
  63. ----
  64. [[components.fields.databinding]]
  65. == Binding Fields to Data
  66. Fields can be grouped into _forms_ and coupled with business data objects with
  67. the [classname]#Binder# class. When a field is bound to a property using
  68. [classname]#Binder#, it gets its default value from the property, and
  69. is stored to the property either manually via the [methodname]#Binder.save# method,
  70. or automatically every time the value changes.
  71. [source, java]
  72. ----
  73. class Person {
  74. private String name;
  75. public String getName() { /* ... */ }
  76. public void setName(String) { /* ... */ }
  77. }
  78. TextField nameField = new TextField();
  79. Binder<Person> binder = new Binder<>();
  80. // Bind nameField to the Person.name property
  81. // by specifying its getter and setter
  82. binder.bind(nameField, Person::getName, Person::setName);
  83. // Bind an actual concrete Person instance.
  84. // After this, whenever the user changes the value
  85. // of nameField, p.setName is automatically called.
  86. Person p = new Person();
  87. binder.bind(p);
  88. ----
  89. For more information on data binding, see <<dummy/../../../framework/datamodel/datamodel-forms.asciidoc#datamodel.forms,"Binding Data to Forms">>
  90. == Validating Field Values
  91. User input may be syntactically or semantically invalid.
  92. [classname]#Binder# allows adding a chain of one or more __validators__ for
  93. automatically checking the validity of the input before storing it to the data
  94. object. You can add validators to fields by calling the [methodname]#withValidator#
  95. method on the [interfacename]#Binding# object returned by [methodname]#Binder.forField#.
  96. There are several built-in validators in the Framework, such as the [classname]#StringLengthValidator# used below.
  97. [source, java]
  98. ----
  99. binder.forField(nameField)
  100. .withValidator(new StringLengthValidator(
  101. "Name must be between 2 and 20 characters long",
  102. 2, 20))
  103. .bind(Person::getName, Person::setName);
  104. ----
  105. Failed validation is by default indicated with the error indicator of the field, described in
  106. <<dummy/../../../framework/application/application-errors#application.errors.error-indicator,"Error
  107. Indicator and Message">>. Hovering mouse on the field displays the error message
  108. returned by the validator. If any value in a set of bound fields fails validation,
  109. none of the field values are saved into the bound property until the validation
  110. passes.
  111. === Implementing Custom Validators
  112. Validators implement the [interfacename]#Validator# interface that simply
  113. extends [interfacename]#java.util.function.Function#, returning a special type
  114. called [interfacename]#Result#. This return type represents the validation outcome:
  115. whether or not the given input was valid.
  116. [source, java]
  117. ----
  118. class MyValidator implements Validator<String> {
  119. @Override
  120. public Result<String> apply(String value, ValueContext context) {
  121. if(input.length() == 6) {
  122. return Result.ok(input);
  123. } else {
  124. return Result.error(
  125. "Must be exactly six characters long");
  126. }
  127. }
  128. }
  129. ----
  130. Because [methodname]#Result.ok# takes the valid value as an argument, a validator
  131. can also do some sanitization on valid inputs, such as removing leading and
  132. trailing whitespace from a string. Since [interfacename]#Validator# is a functional
  133. interface, you can often simply write a lambda expression instead of a full class
  134. declaration. There is also an [methodname]#withValidator# overload that creates a
  135. validator from a boolean function and an error message.
  136. [source, java]
  137. ----
  138. binder.forField(nameField)
  139. .withValidator(name -> name.length() < 20,
  140. "Name must be less than 20 characters long")
  141. .bind(Person::getName, Person::setName);
  142. ----
  143. == Converting Field Values
  144. Field values are always of some particular type. For example,
  145. [classname]#TextField# allows editing [classname]#String# values. When bound to
  146. a data source, the type of the source property can be something different,
  147. say an [classname]#Integer#. __Converters__ are used for converting the values
  148. between the presentation and the model. Their usage is described in
  149. <<dummy/../../../framework/datamodel/datamodel-forms.asciidoc#datamodel.forms.conversion,"Conversion">>.
  150. (((range="endofrange", startref="term.components.fields")))