public class Form<T> extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
Form.Display
Defines a form element's display name.
|
static class |
Form.Field
A form field.
|
Constructor and Description |
---|
Form(Class<T> clazz,
MessagesApi messagesApi,
Formatters formatters,
javax.validation.ValidatorFactory validatorFactory,
com.typesafe.config.Config config)
Creates a new
Form . |
Form(String rootName,
Class<T> clazz,
Class<?>[] groups,
MessagesApi messagesApi,
Formatters formatters,
javax.validation.ValidatorFactory validatorFactory,
com.typesafe.config.Config config) |
Form(String rootName,
Class<T> clazz,
Class<?> group,
MessagesApi messagesApi,
Formatters formatters,
javax.validation.ValidatorFactory validatorFactory,
com.typesafe.config.Config config) |
Form(String rootName,
Class<T> clazz,
Map<String,String> data,
List<ValidationError> errors,
Optional<T> value,
Class<?>[] groups,
MessagesApi messagesApi,
Formatters formatters,
javax.validation.ValidatorFactory validatorFactory,
com.typesafe.config.Config config)
Creates a new
Form . |
Form(String rootName,
Class<T> clazz,
Map<String,String> data,
List<ValidationError> errors,
Optional<T> value,
Class<?>[] groups,
MessagesApi messagesApi,
Formatters formatters,
javax.validation.ValidatorFactory validatorFactory,
com.typesafe.config.Config config,
Lang lang)
Creates a new
Form . |
Form(String rootName,
Class<T> clazz,
Map<String,String> data,
List<ValidationError> errors,
Optional<T> value,
Class<?>[] groups,
MessagesApi messagesApi,
Formatters formatters,
javax.validation.ValidatorFactory validatorFactory,
com.typesafe.config.Config config,
Lang lang,
boolean directFieldAccess)
Creates a new
Form . |
Form(String rootName,
Class<T> clazz,
Map<String,String> data,
List<ValidationError> errors,
Optional<T> value,
Class<?> group,
MessagesApi messagesApi,
Formatters formatters,
javax.validation.ValidatorFactory validatorFactory,
com.typesafe.config.Config config) |
Form(String rootName,
Class<T> clazz,
Map<String,String> data,
List<ValidationError> errors,
Optional<T> value,
MessagesApi messagesApi,
Formatters formatters,
javax.validation.ValidatorFactory validatorFactory,
com.typesafe.config.Config config) |
Form(String rootName,
Class<T> clazz,
Map<String,String> data,
Map<String,Http.MultipartFormData.FilePart<?>> files,
List<ValidationError> errors,
Optional<T> value,
Class<?>[] groups,
MessagesApi messagesApi,
Formatters formatters,
javax.validation.ValidatorFactory validatorFactory,
com.typesafe.config.Config config)
Creates a new
Form . |
Form(String rootName,
Class<T> clazz,
Map<String,String> data,
Map<String,Http.MultipartFormData.FilePart<?>> files,
List<ValidationError> errors,
Optional<T> value,
Class<?>[] groups,
MessagesApi messagesApi,
Formatters formatters,
javax.validation.ValidatorFactory validatorFactory,
com.typesafe.config.Config config,
Lang lang)
Creates a new
Form . |
Form(String rootName,
Class<T> clazz,
Map<String,String> data,
Map<String,Http.MultipartFormData.FilePart<?>> files,
List<ValidationError> errors,
Optional<T> value,
Class<?>[] groups,
MessagesApi messagesApi,
Formatters formatters,
javax.validation.ValidatorFactory validatorFactory,
com.typesafe.config.Config config,
Lang lang,
boolean directFieldAccess)
Creates a new
Form . |
Form(String rootName,
Class<T> clazz,
Map<String,String> data,
Map<String,Http.MultipartFormData.FilePart<?>> files,
List<ValidationError> errors,
Optional<T> value,
Class<?> group,
MessagesApi messagesApi,
Formatters formatters,
javax.validation.ValidatorFactory validatorFactory,
com.typesafe.config.Config config) |
Form(String rootName,
Class<T> clazz,
Map<String,String> data,
Map<String,Http.MultipartFormData.FilePart<?>> files,
List<ValidationError> errors,
Optional<T> value,
MessagesApi messagesApi,
Formatters formatters,
javax.validation.ValidatorFactory validatorFactory,
com.typesafe.config.Config config) |
Form(String rootName,
Class<T> clazz,
MessagesApi messagesApi,
Formatters formatters,
javax.validation.ValidatorFactory validatorFactory,
com.typesafe.config.Config config) |
Modifier and Type | Method and Description |
---|---|
Form.Field |
apply(String key)
Retrieves a field.
|
Form.Field |
apply(String key,
Lang lang)
Retrieves a field.
|
Form<T> |
bind(Lang lang,
TypedMap attrs,
com.fasterxml.jackson.databind.JsonNode data,
String... allowedFields)
Binds Json data to this form - that is, handles form submission.
|
Form<T> |
bind(Lang lang,
TypedMap attrs,
Map<String,String> data,
Map<String,Http.MultipartFormData.FilePart<?>> files,
String... allowedFields)
Binds data to this form - that is, handles form submission.
|
Form<T> |
bind(Lang lang,
TypedMap attrs,
Map<String,String> data,
String... allowedFields)
Binds data to this form - that is, handles form submission.
|
Form<T> |
bindFromRequest(Http.Request request,
String... allowedFields)
Binds request data to this form - that is, handles form submission.
|
Form<T> |
bindFromRequestData(Lang lang,
TypedMap attrs,
Map<String,String[]> requestData,
Map<String,Http.MultipartFormData.FilePart<?>> requestFileData,
String... allowedFields)
Binds request data to this form - that is, handles form submission.
|
Form<T> |
bindFromRequestData(Lang lang,
TypedMap attrs,
Map<String,String[]> requestData,
String... allowedFields)
Binds request data to this form - that is, handles form submission.
|
protected T |
blankInstance() |
Form<T> |
discardingErrors() |
Optional<ValidationError> |
error(String key) |
List<ValidationError> |
errors()
Returns all errors.
|
List<ValidationError> |
errors(String key) |
com.fasterxml.jackson.databind.JsonNode |
errorsAsJson() |
com.fasterxml.jackson.databind.JsonNode |
errorsAsJson(Lang lang)
Returns the form errors serialized as Json using the given Lang.
|
Form.Field |
field(String key)
Retrieves a field.
|
Form.Field |
field(String key,
Lang lang)
Retrieves a field.
|
Map<String,Http.MultipartFormData.FilePart<?>> |
files() |
Form<T> |
fill(T value)
Populates this form with an existing value, used for edit forms.
|
protected void |
fillDataWith(Map<String,String> data,
Map<String,String[]> urlFormEncoded) |
T |
get()
Gets the concrete value only if the submission was a success.
|
T |
get(Lang lang)
Gets the concrete value only if the submission was a success.
|
protected Object[] |
getArgumentsForConstraint(String objectName,
String field,
javax.validation.ConstraintViolation<Object> violation) |
Class<T> |
getBackedType() |
protected String |
getMessageForConstraintViolation(javax.validation.ConstraintViolation<Object> violation)
When dealing with @ValidateWith or @ValidatePayloadWith annotations, and message parameter is
not used in the annotation, extract the message from validator's getErrorMessageKey() method
|
Optional<ValidationError> |
globalError()
Retrieves the first global error (an error without any key), if it exists.
|
List<ValidationError> |
globalErrors()
Retrieve all global errors - errors without a key.
|
boolean |
hasErrors() |
boolean |
hasGlobalErrors() |
Optional<Lang> |
lang() |
String |
name() |
Map<String,String> |
rawData() |
protected Map<String,String> |
requestData(Http.Request request) |
protected Map<String,Http.MultipartFormData.FilePart<?>> |
requestFileData(Http.Request request) |
protected <A> Map<String,Http.MultipartFormData.FilePart<?>> |
resolveDuplicateFilePartKeys(List<Http.MultipartFormData.FilePart<A>> fileParts) |
String |
toString() |
Optional<T> |
value() |
Form<T> |
withDirectFieldAccess(boolean directFieldAccess)
Sets if during binding fields of the form should be accessed directly or via getters.
|
Form<T> |
withError(String key,
String error) |
Form<T> |
withError(String key,
String error,
List<Object> args) |
Form<T> |
withError(ValidationError error) |
Form<T> |
withGlobalError(String error) |
Form<T> |
withGlobalError(String error,
List<Object> args) |
Form<T> |
withLang(Lang lang)
A copy of this form with the given lang set which is used for formatting when retrieving a
field (via
field(String) or apply(String) ) and for translations in errorsAsJson() . |
public Form(Class<T> clazz, MessagesApi messagesApi, Formatters formatters, javax.validation.ValidatorFactory validatorFactory, com.typesafe.config.Config config)
Form
. Consider using a FormFactory
rather than this
constructor.clazz
- wrapped classmessagesApi
- messagesApi component.formatters
- formatters component.validatorFactory
- validatorFactory component.config
- config component.public Form(String rootName, Class<T> clazz, MessagesApi messagesApi, Formatters formatters, javax.validation.ValidatorFactory validatorFactory, com.typesafe.config.Config config)
public Form(String rootName, Class<T> clazz, Class<?> group, MessagesApi messagesApi, Formatters formatters, javax.validation.ValidatorFactory validatorFactory, com.typesafe.config.Config config)
public Form(String rootName, Class<T> clazz, Class<?>[] groups, MessagesApi messagesApi, Formatters formatters, javax.validation.ValidatorFactory validatorFactory, com.typesafe.config.Config config)
public Form(String rootName, Class<T> clazz, Map<String,String> data, List<ValidationError> errors, Optional<T> value, MessagesApi messagesApi, Formatters formatters, javax.validation.ValidatorFactory validatorFactory, com.typesafe.config.Config config)
public Form(String rootName, Class<T> clazz, Map<String,String> data, Map<String,Http.MultipartFormData.FilePart<?>> files, List<ValidationError> errors, Optional<T> value, MessagesApi messagesApi, Formatters formatters, javax.validation.ValidatorFactory validatorFactory, com.typesafe.config.Config config)
public Form(String rootName, Class<T> clazz, Map<String,String> data, List<ValidationError> errors, Optional<T> value, Class<?> group, MessagesApi messagesApi, Formatters formatters, javax.validation.ValidatorFactory validatorFactory, com.typesafe.config.Config config)
public Form(String rootName, Class<T> clazz, Map<String,String> data, Map<String,Http.MultipartFormData.FilePart<?>> files, List<ValidationError> errors, Optional<T> value, Class<?> group, MessagesApi messagesApi, Formatters formatters, javax.validation.ValidatorFactory validatorFactory, com.typesafe.config.Config config)
public Form(String rootName, Class<T> clazz, Map<String,String> data, List<ValidationError> errors, Optional<T> value, Class<?>[] groups, MessagesApi messagesApi, Formatters formatters, javax.validation.ValidatorFactory validatorFactory, com.typesafe.config.Config config)
Form
. Consider using a FormFactory
rather than this
constructor.rootName
- the root name.clazz
- wrapped classdata
- the current form data (used to display the form)errors
- the collection of errors associated with this formvalue
- optional concrete value of type T
if the form submission was
successfulgroups
- the array of classes with the groups.messagesApi
- needed to look up various messagesformatters
- used for parsing and printing form fieldsvalidatorFactory
- the validatorFactory component.config
- the config component.public Form(String rootName, Class<T> clazz, Map<String,String> data, Map<String,Http.MultipartFormData.FilePart<?>> files, List<ValidationError> errors, Optional<T> value, Class<?>[] groups, MessagesApi messagesApi, Formatters formatters, javax.validation.ValidatorFactory validatorFactory, com.typesafe.config.Config config)
Form
. Consider using a FormFactory
rather than this
constructor.rootName
- the root name.clazz
- wrapped classdata
- the current form data (used to display the form)files
- the current form file dataerrors
- the collection of errors associated with this formvalue
- optional concrete value of type T
if the form submission was
successfulgroups
- the array of classes with the groups.messagesApi
- needed to look up various messagesformatters
- used for parsing and printing form fieldsvalidatorFactory
- the validatorFactory component.config
- the config component.public Form(String rootName, Class<T> clazz, Map<String,String> data, List<ValidationError> errors, Optional<T> value, Class<?>[] groups, MessagesApi messagesApi, Formatters formatters, javax.validation.ValidatorFactory validatorFactory, com.typesafe.config.Config config, Lang lang)
Form
. Consider using a FormFactory
rather than this
constructor.rootName
- the root name.clazz
- wrapped classdata
- the current form data (used to display the form)errors
- the collection of errors associated with this formvalue
- optional concrete value of type T
if the form submission was
successfulgroups
- the array of classes with the groups.messagesApi
- needed to look up various messagesformatters
- used for parsing and printing form fieldsvalidatorFactory
- the validatorFactory component.config
- the config component.lang
- used for formatting when retrieving a field (via field(String)
or apply(String)
) and for translations in errorsAsJson()
public Form(String rootName, Class<T> clazz, Map<String,String> data, Map<String,Http.MultipartFormData.FilePart<?>> files, List<ValidationError> errors, Optional<T> value, Class<?>[] groups, MessagesApi messagesApi, Formatters formatters, javax.validation.ValidatorFactory validatorFactory, com.typesafe.config.Config config, Lang lang)
Form
. Consider using a FormFactory
rather than this
constructor.rootName
- the root name.clazz
- wrapped classdata
- the current form data (used to display the form)files
- the current form file dataerrors
- the collection of errors associated with this formvalue
- optional concrete value of type T
if the form submission was
successfulgroups
- the array of classes with the groups.messagesApi
- needed to look up various messagesformatters
- used for parsing and printing form fieldsvalidatorFactory
- the validatorFactory component.config
- the config component.lang
- used for formatting when retrieving a field (via field(String)
or apply(String)
) and for translations in errorsAsJson()
public Form(String rootName, Class<T> clazz, Map<String,String> data, List<ValidationError> errors, Optional<T> value, Class<?>[] groups, MessagesApi messagesApi, Formatters formatters, javax.validation.ValidatorFactory validatorFactory, com.typesafe.config.Config config, Lang lang, boolean directFieldAccess)
Form
. Consider using a FormFactory
rather than this
constructor.rootName
- the root name.clazz
- wrapped classdata
- the current form data (used to display the form)errors
- the collection of errors associated with this formvalue
- optional concrete value of type T
if the form submission was
successfulgroups
- the array of classes with the groups.messagesApi
- needed to look up various messagesformatters
- used for parsing and printing form fieldsvalidatorFactory
- the validatorFactory component.config
- the config component.lang
- used for formatting when retrieving a field (via field(String)
or apply(String)
) and for translations in errorsAsJson()
directFieldAccess
- access fields of form directly during binding instead of using getterspublic Form(String rootName, Class<T> clazz, Map<String,String> data, Map<String,Http.MultipartFormData.FilePart<?>> files, List<ValidationError> errors, Optional<T> value, Class<?>[] groups, MessagesApi messagesApi, Formatters formatters, javax.validation.ValidatorFactory validatorFactory, com.typesafe.config.Config config, Lang lang, boolean directFieldAccess)
Form
. Consider using a FormFactory
rather than this
constructor.rootName
- the root name.clazz
- wrapped classdata
- the current form data (used to display the form)files
- the current form file dataerrors
- the collection of errors associated with this formvalue
- optional concrete value of type T
if the form submission was
successfulgroups
- the array of classes with the groups.messagesApi
- needed to look up various messagesformatters
- used for parsing and printing form fieldsvalidatorFactory
- the validatorFactory component.config
- the config component.lang
- used for formatting when retrieving a field (via field(String)
or apply(String)
) and for translations in errorsAsJson()
directFieldAccess
- access fields of form directly during binding instead of using gettersprotected T blankInstance()
protected Map<String,String> requestData(Http.Request request)
protected void fillDataWith(Map<String,String> data, Map<String,String[]> urlFormEncoded)
protected Map<String,Http.MultipartFormData.FilePart<?>> requestFileData(Http.Request request)
protected <A> Map<String,Http.MultipartFormData.FilePart<?>> resolveDuplicateFilePartKeys(List<Http.MultipartFormData.FilePart<A>> fileParts)
public Form<T> bindFromRequest(Http.Request request, String... allowedFields)
request
- the request to bind data from.allowedFields
- the fields that should be bound to the form, all fields if not specified.public Form<T> bindFromRequestData(Lang lang, TypedMap attrs, Map<String,String[]> requestData, String... allowedFields)
lang
- used for validators and formatters during binding and is part of Constraints.ValidationPayload
. Later also used for formatting when retrieving a field (via field(String)
or apply(String)
) and for translations in errorsAsJson()
.
For these methods the lang can be change via withLang(Lang)
.attrs
- will be passed to validators via Constraints.ValidationPayload
requestData
- the map of data to bind fromallowedFields
- the fields that should be bound to the form, all fields if not specified.public Form<T> bindFromRequestData(Lang lang, TypedMap attrs, Map<String,String[]> requestData, Map<String,Http.MultipartFormData.FilePart<?>> requestFileData, String... allowedFields)
lang
- used for validators and formatters during binding and is part of Constraints.ValidationPayload
. Later also used for formatting when retrieving a field (via field(String)
or apply(String)
) and for translations in errorsAsJson()
.
For these methods the lang can be change via withLang(Lang)
.attrs
- will be passed to validators via Constraints.ValidationPayload
requestData
- the map of data to bind fromrequestFileData
- the map of file data to bind fromallowedFields
- the fields that should be bound to the form, all fields if not specified.public Form<T> bind(Lang lang, TypedMap attrs, com.fasterxml.jackson.databind.JsonNode data, String... allowedFields)
lang
- used for validators and formatters during binding and is part of Constraints.ValidationPayload
. Later also used for formatting when retrieving a field (via field(String)
or apply(String)
) and for translations in errorsAsJson()
.
For these methods the lang can be change via withLang(Lang)
.attrs
- will be passed to validators via Constraints.ValidationPayload
data
- data to submitallowedFields
- the fields that should be bound to the form, all fields if not specified.protected Object[] getArgumentsForConstraint(String objectName, String field, javax.validation.ConstraintViolation<Object> violation)
protected String getMessageForConstraintViolation(javax.validation.ConstraintViolation<Object> violation)
violation
- the constraint violation.public Form<T> bind(Lang lang, TypedMap attrs, Map<String,String> data, String... allowedFields)
lang
- used for validators and formatters during binding and is part of Constraints.ValidationPayload
. Later also used for formatting when retrieving a field (via field(String)
or apply(String)
) and for translations in errorsAsJson()
.
For these methods the lang can be change via withLang(Lang)
.attrs
- will be passed to validators via Constraints.ValidationPayload
data
- data to submitallowedFields
- the fields that should be bound to the form, all fields if not specified.public Form<T> bind(Lang lang, TypedMap attrs, Map<String,String> data, Map<String,Http.MultipartFormData.FilePart<?>> files, String... allowedFields)
lang
- used for validators and formatters during binding and is part of Constraints.ValidationPayload
. Later also used for formatting when retrieving a field (via field(String)
or apply(String)
) and for translations in errorsAsJson()
.
For these methods the lang can be change via withLang(Lang)
.attrs
- will be passed to validators via Constraints.ValidationPayload
data
- data to submitallowedFields
- the fields that should be bound to the form, all fields if not specified.public Map<String,String> rawData()
files()
to access files.public Map<String,Http.MultipartFormData.FilePart<?>> files()
rawData()
to access other form data.public String name()
public Optional<T> value()
public Form<T> fill(T value)
value
- existing value of type T
used to fill this formpublic boolean hasErrors()
true
if there are any errors related to this form.public boolean hasGlobalErrors()
true
if there any global errors related to this form.public List<ValidationError> globalErrors()
public Optional<ValidationError> globalError()
public List<ValidationError> errors()
public List<ValidationError> errors(String key)
key
- the field name associated with the error.public Optional<ValidationError> error(String key)
key
- the field name associated with the error.public com.fasterxml.jackson.databind.JsonNode errorsAsJson()
public com.fasterxml.jackson.databind.JsonNode errorsAsJson(Lang lang)
lang
- the language to use.public T get()
value()
instead.IllegalStateException
- if there are errors binding the form, including the errors as
JSON in the messagepublic T get(Lang lang)
value()
instead.lang
- if an IllegalStateException gets thrown it's used to translate the form errors
within that exceptionIllegalStateException
- if there are errors binding the form, including the errors as
JSON in the messagepublic Form<T> withError(ValidationError error)
error
- the ValidationError
to add to the returned form.public Form<T> withError(String key, String error, List<Object> args)
key
- the error keyerror
- the error messageargs
- the error argumentspublic Form<T> withError(String key, String error)
key
- the error keyerror
- the error messagepublic Form<T> withGlobalError(String error, List<Object> args)
error
- the global error messageargs
- the global error argumentspublic Form<T> withGlobalError(String error)
error
- the global error messagepublic Form<T> discardingErrors()
public Form.Field apply(String key)
key
- field namepublic Form.Field apply(String key, Lang lang)
key
- field namelang
- the language to use for the formatterpublic Form.Field field(String key)
key
- field namepublic Form.Field field(String key, Lang lang)
key
- field namelang
- used for formattingpublic Optional<Lang> lang()
field(String)
or
apply(String)
) and for translations in errorsAsJson()
. For these methods
the lang can be change via withLang(Lang)
.public Form<T> withLang(Lang lang)
field(String)
or apply(String)
) and for translations in errorsAsJson()
.public Form<T> withDirectFieldAccess(boolean directFieldAccess)
directFieldAccess
- true
enables direct field access during form binding, false
disables it and uses getters instead. If null
falls back to config default.