Helper to manage HTML form description, submission and validation.
For example, a form handling a User case class submission:
import play.api.data._
import play.api.data.Forms._
import play.api.data.format.Formats._
val userForm = Form(
mapping(
"name" -> of[String],
"age" -> of[Int],
"email" -> of[String]
)(User.apply)(User.unapply)
)
Type parameters
- T
-
the type managed by this form
Value parameters
- data
-
the current form data, used to display the form
- errors
-
the collection of errors associated with this form
- mapping
-
the form mapping, which describes all form fields
- value
-
a concrete value of type
Tif the form submission was successful
Attributes
- Companion
- object
- Graph
-
- Supertypes
Members list
Value members
Concrete methods
Retrieves a field.
Retrieves a field.
For example:
val usernameField = userForm("username")
Value parameters
- key
-
the field name
Attributes
- Returns
-
the field, returned even if the field does not exist
Binds data to this form, i.e. handles form submission.
Binds data to this form, i.e. handles form submission.
Value parameters
- data
-
the data to submit
Attributes
- Returns
-
a copy of this form, filled with the new data
Binds data to this form, i.e. handles form submission.
Binds data to this form, i.e. handles form submission.
Value parameters
- data
-
Json data to submit
- maxChars
-
The maximum number of chars allowed to be used in the intermediate map representation of the JSON.
parse.DefaultMaxTextLengthis recommended to passed for this parameter.
Attributes
- Returns
-
a copy of this form, filled with the new data
Binds data to this form, i.e. handles form submission.
Binds data to this form, i.e. handles form submission.
Value parameters
- data
-
Json data to submit
- maxChars
-
The maximum number of chars allowed to be used in the intermediate map representation of the JSON.
parse.DefaultMaxTextLengthis recommended to passed for this parameter. - maxDepth
-
The maximum level of nesting for JSON objects and arrays.
Attributes
- Returns
-
a copy of this form, filled with the new data
Binds request data to this form, i.e. handles form submission.
Binds request data to this form, i.e. handles form submission.
Attributes
- Returns
-
a copy of this form filled with the new data
Discards this form’s errors
Discards this form’s errors
Attributes
- Returns
-
a copy of this form without errors
Retrieve the first error for this key.
Retrieve the first error for this key.
Value parameters
- key
-
field name.
Attributes
Retrieve all errors for this key.
Retrieve all errors for this key.
Value parameters
- key
-
field name.
Attributes
Returns the form errors serialized as Json.
Returns the form errors serialized as Json.
Attributes
Fills this form with a existing value, used for edit forms.
Fills this form with a existing value, used for edit forms.
Value parameters
- value
-
an existing value of type
T, used to fill this form
Attributes
- Returns
-
a copy of this form filled with the new data
Fills this form with a existing value, and performs a validation.
Fills this form with a existing value, and performs a validation.
Value parameters
- value
-
an existing value of type
T, used to fill this form
Attributes
- Returns
-
a copy of this form filled with the new data
Handles form results. Either the form has errors, or the submission was a success and a concrete value is available.
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)
)
Type parameters
- R
-
common result type
Value parameters
- hasErrors
-
a function to handle forms with errors
- success
-
a function to handle form submission success
Attributes
- Returns
-
a result
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("")} />
}
Type parameters
- R
-
result type
Value parameters
- handler
-
field handler (transform the field to
R) - key
-
field name
Attributes
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.
Attributes
Retrieves the first global error, if it exists, i.e. an error without any key.
Retrieves the first global error, if it exists, i.e. an error without any key.
Attributes
- Returns
-
an error
Retrieves all global errors, i.e. errors without a key.
Retrieves all global errors, i.e. errors without a key.
Attributes
- Returns
-
all global errors
Returns true if there is an error related to this form.
Returns true if there is an error related to this form.
Attributes
Returns true if there is a global error related to this form.
Returns true if there is a global error related to this form.
Attributes
Adds an error to this form
Adds an error to this form
Value parameters
- error
-
Error to add
Attributes
- Returns
-
a copy of this form with the added error
Convenient overloaded method adding an error to this form
Convenient overloaded method adding an error to this form
Value parameters
- args
-
Error message arguments
- key
-
Key of the field having the error
- message
-
Error message
Attributes
- Returns
-
a copy of this form with the added error
Adds a global error to this form
Adds a global error to this form
Value parameters
- args
-
Error message arguments
- message
-
Error message
Attributes
- Returns
-
a copy of this form with the added global error
Concrete fields
Constraints associated with this form, indexed by field name.
Constraints associated with this form, indexed by field name.