:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

The `maxLength` element specifies the maximum length of the value that can be held by the property.
This corresponds to the number of characters that can be entered through the text field:

* for properties, the element can be specified using either the `@Property` annotation, or a meta-annotation

* for parameters, the element can be specified using either the `@Parameter` annotation, or a meta-annotation.

This semantic applies only to strings.

[IMPORTANT]
====
For domain entities this semantic should be accompanied with `@Column(maxLength=...)` so that the persistence layer implements the same constraint.
====

[NOTE]
====
`@Property#maxLength()` and `@PropertyLayout#typicalLength()` are not related - the first is an element that prevents the property from holding more than the specified number of characters, the latter is a layout hint relating to the size of the textfield.
====



== Annotated

The `maxLength` element can be specified explicitly using an annotation:

* for properties, using `@Property#maxLength()`:
+
[source,java,indent=0]
----
include::PropertyMaxLengthVm.java[tags=annotation]
----
<.> explicitly specifies the maximum length that can be entered for the property

* for parameters, using `@Parameter#maxLength()`:
+
[source,java,indent=0]
----
include::PropertyMaxLengthVm_updateWithParameterLayout.java[tags=annotation]
----
<.> explicitly specifies the maximum length that can be entered for the property

The annotation could also be applied to a mixin, but would have little effect because mixin properties are always read-only.

== Meta-annotated

The `maxLength` element can also be specified using a custom meta-annotation:

* the meta-annotation is defined as:
+
[source,java]
----
include::MaxLength10MetaAnnotation.java[tags=class]
----
<.> annotated for a property ...
<.> \... and for a parameter.
<.> annotation can be applied to a property (either its getter or the field directly)
<.> annotation can be applied to a parameter

* and can be applied to a property:
+
[source,java,indent=0]
----
include::PropertyMaxLengthVm.java[tags=meta-annotated]
----
<.> semantic is inherited from the meta-annotation

* or can be applied to a parameter:
+
[source,java,indent=0]
----
include::PropertyMaxLengthVm_updateWithMetaAnnotation.java[tags=meta-annotation]
----
<.> semantic is inherited from the meta-annotation


=== Meta-annotated Overridden

The meta-annotation can itself be overridden:

* in a property:
+
[source,java,indent=0]
----
include::PropertyMaxLengthVm.java[tags=meta-annotated-overridden]
----
<.> semantic from meta-annotation ...
<.> \... is overridden by the `@Property` annotation

* or a parameter:
+
[source,java,indent=0]
----
include::PropertyMaxLengthVm_updateWithMetaAnnotationOverridden.java[tags=meta-annotation-overridden]
----
<.> semantic from meta-annotation ...
<.> \... is overridden by the `@Parameter` annotation
