case class Form[T](mapping: Mapping[T], data: Map[String, String], errors: Seq[FormError], value: Option[T]) extends Product with Serializable
- Alphabetic
- By Inheritance
- Form
- Serializable
- Product
- Equals
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Instance Constructors
- new Form(mapping: Mapping[T], data: Map[String, String], errors: Seq[FormError], value: Option[T])
- mapping
the form mapping, which describes all form fields
- data
the current form data, used to display the form
- errors
the collection of errors associated with this form
- value
a concrete value of type
T
if the form submission was successful
Value Members
- def apply(key: String): Field
Retrieves a field.
Retrieves a field.
For example:
val usernameField = userForm("username")
- key
the field name
- returns
the field, returned even if the field does not exist
- def bind(data: JsValue, maxChars: Long, maxDepth: Int): Form[T]
Binds data to this form, i.e.
Binds data to this form, i.e. handles form submission.
- data
Json data to submit
- maxChars
The maximum number of chars allowed to be used in the intermediate map representation of the JSON.
parse.DefaultMaxTextLength
is recommended to passed for this parameter.- maxDepth
The maximum level of nesting for JSON objects and arrays.
- returns
a copy of this form, filled with the new data
- def bind(data: JsValue, maxChars: Long): Form[T]
Binds data to this form, i.e.
Binds data to this form, i.e. handles form submission.
- data
Json data to submit
- maxChars
The maximum number of chars allowed to be used in the intermediate map representation of the JSON.
parse.DefaultMaxTextLength
is recommended to passed for this parameter.- returns
a copy of this form, filled with the new data
- def bind(data: Map[String, String]): Form[T]
Binds data to this form, i.e.
Binds data to this form, i.e. handles form submission.
- data
the data to submit
- returns
a copy of this form, filled with the new data
- def bindFromRequest(data: Map[String, Seq[String]]): Form[T]
- def bindFromRequest()(implicit request: Request[_], formBinding: FormBinding): Form[T]
Binds request data to this form, i.e.
Binds request data to this form, i.e. handles form submission.
- returns
a copy of this form filled with the new data
- val constraints: Map[String, Seq[(String, Seq[Any])]]
Constraints associated with this form, indexed by field name.
- val data: Map[String, String]
- def discardingErrors: Form[T]
Discards this form’s errors
Discards this form’s errors
- returns
a copy of this form without errors
- def error(key: String): Option[FormError]
Retrieve the first error for this key.
Retrieve the first error for this key.
- key
field name.
- def errors(key: String): Seq[FormError]
Retrieve all errors for this key.
Retrieve all errors for this key.
- key
field name.
- val errors: Seq[FormError]
- def errorsAsJson(implicit provider: MessagesProvider): JsValue
Returns the form errors serialized as Json.
- def fill(value: T): Form[T]
Fills this form with a existing value, used for edit forms.
Fills this form with a existing value, used for edit forms.
- value
an existing value of type
T
, used to fill this form- returns
a copy of this form filled with the new data
- def fillAndValidate(value: T): Form[T]
Fills this form with a existing value, and performs a validation.
Fills this form with a existing value, and performs a validation.
- value
an existing value of type
T
, used to fill this form- returns
a copy of this form filled with the new data
- def fold[R](hasErrors: (Form[T]) => R, success: (T) => R): R
Handles form results.
Handles form results. Either the form has errors, or the submission was a success and a concrete value is available.
For example:
anyForm.bindFromRequest().fold( f => redisplayForm(f), t => handleValidFormSubmission(t) )
- R
common result type
- hasErrors
a function to handle forms with errors
- success
a function to handle form submission success
- returns
a result
R
.
- def forField[R](key: String)(handler: (Field) => R): R
Applies a function for a field.
Applies a function for a field.
For example:
userForm.forField("username") { field => <input type="text" name={field.name} value={field.value.getOrElse("")} /> }
- R
result type
- key
field name
- handler
field handler (transform the field to
R
)
- val formats: Map[String, (String, Seq[Any])]
Formats associated to this form, indexed by field name.
Formats associated to this form, indexed by field name. *
- def get: T
Returns the concrete value, if the submission was a success.
Returns the concrete value, if the submission was a success.
Note that this method fails with an Exception if this form has errors.
- def globalError: Option[FormError]
Retrieves the first global error, if it exists, i.e.
Retrieves the first global error, if it exists, i.e. an error without any key.
- returns
an error
- def globalErrors: Seq[FormError]
Retrieves all global errors, i.e.
Retrieves all global errors, i.e. errors without a key.
- returns
all global errors
- def hasErrors: Boolean
Returns
true
if there is an error related to this form. - def hasGlobalErrors: Boolean
Returns
true
if there is a global error related to this form. - val mapping: Mapping[T]
- def productElementNames: Iterator[String]
- Definition Classes
- Product
- val value: Option[T]
- def withError(key: String, message: String, args: Any*): Form[T]
Convenient overloaded method adding an error to this form
Convenient overloaded method adding an error to this form
- key
Key of the field having the error
- message
Error message
- args
Error message arguments
- returns
a copy of this form with the added error
- def withError(error: FormError): Form[T]
Adds an error to this form
Adds an error to this form
- error
Error to add
- returns
a copy of this form with the added error
- def withGlobalError(message: String, args: Any*): Form[T]
Adds a global error to this form
Adds a global error to this form
- message
Error message
- args
Error message arguments
- returns
a copy of this form with the added global error
Deprecated Value Members
- def bind(data: JsValue): Form[T]
Binds data to this form, i.e.
Binds data to this form, i.e. handles form submission.
- data
Json data to submit
- returns
a copy of this form, filled with the new data
- Annotations
- @deprecated
- Deprecated
(Since version 2.8.3) Use bind(JsValue, Int) instead to specify the maximum chars that should be consumed by the flattened form representation of the JSON
Helper to manage HTML form description, submission and validation.
For example, a form handling a
User
case class submission:the type managed by this form
the form mapping, which describes all form fields
the current form data, used to display the form
the collection of errors associated with this form
a concrete value of type
T
if the form submission was successful