Class/Object

play.api.data

Form

Related Docs: object Form | package data

Permalink

case class Form[T](mapping: Mapping[T], data: Map[String, String], errors: Seq[FormError], value: Option[T]) extends Product with Serializable

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)
)
T

the type managed by this form

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

Source
Form.scala
Linear Supertypes
Serializable, Serializable, Product, Equals, AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Form
  2. Serializable
  3. Serializable
  4. Product
  5. Equals
  6. AnyRef
  7. Any
  1. Hide All
  2. Show all
Visibility
  1. Public
  2. All

Instance Constructors

  1. new Form(mapping: Mapping[T], data: Map[String, String], errors: Seq[FormError], value: Option[T])

    Permalink

    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

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. def apply(key: String): Field

    Permalink

    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

  5. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  6. def bind(data: JsValue): Form[T]

    Permalink

    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

  7. def bind(data: Map[String, String]): Form[T]

    Permalink

    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

  8. def bindFromRequest(data: Map[String, Seq[String]]): Form[T]

    Permalink
  9. def bindFromRequest()(implicit request: Request[_]): Form[T]

    Permalink

    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

  10. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  11. val constraints: Map[String, Seq[(String, Seq[Any])]]

    Permalink

    Constraints associated with this form, indexed by field name.

  12. val data: Map[String, String]

    Permalink

    the current form data, used to display the form

  13. def discardingErrors: Form[T]

    Permalink

    Discards this form’s errors

    Discards this form’s errors

    returns

    a copy of this form without errors

  14. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  15. def error(key: String): Option[FormError]

    Permalink

    Retrieve the first error for this key.

    Retrieve the first error for this key.

    key

    field name.

  16. def errors(key: String): Seq[FormError]

    Permalink

    Retrieve all errors for this key.

    Retrieve all errors for this key.

    key

    field name.

  17. val errors: Seq[FormError]

    Permalink

    the collection of errors associated with this form

  18. def errorsAsJson(implicit messages: Messages): JsValue

    Permalink

    Returns the form errors serialized as Json.

  19. def fill(value: T): Form[T]

    Permalink

    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

  20. def fillAndValidate(value: T): Form[T]

    Permalink

    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

  21. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  22. def fold[R](hasErrors: (Form[T]) ⇒ R, success: (T) ⇒ R): R

    Permalink

    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.

  23. def forField[R](key: String)(handler: (Field) ⇒ R): R

    Permalink

    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)

  24. val formats: Map[String, (String, Seq[Any])]

    Permalink

    Formats associated to this form, indexed by field name.

    Formats associated to this form, indexed by field name. *

  25. def get: T

    Permalink

    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.

  26. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  27. def globalError: Option[FormError]

    Permalink

    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

  28. def globalErrors: Seq[FormError]

    Permalink

    Retrieves all global errors, i.e.

    Retrieves all global errors, i.e. errors without a key.

    returns

    all global errors

  29. def hasErrors: Boolean

    Permalink

    Returns true if there is an error related to this form.

  30. def hasGlobalErrors: Boolean

    Permalink

    Returns true if there is a global error related to this form.

  31. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  32. val mapping: Mapping[T]

    Permalink

    the form mapping, which describes all form fields

  33. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  34. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  35. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  36. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  37. val value: Option[T]

    Permalink

    a concrete value of type T if the form submission was successful

  38. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  39. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  40. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  41. def withError(key: String, message: String, args: Any*): Form[T]

    Permalink

    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

  42. def withError(error: FormError): Form[T]

    Permalink

    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

  43. def withGlobalError(message: String, args: Any*): Form[T]

    Permalink

    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

Inherited from Serializable

Inherited from Serializable

Inherited from Product

Inherited from Equals

Inherited from AnyRef

Inherited from Any

Ungrouped