Skip to content

Instantly share code, notes, and snippets.

@Timshel
Forked from playxamplez-admin/CODE
Last active December 20, 2015 01:58
Show Gist options
  • Save Timshel/6052496 to your computer and use it in GitHub Desktop.
Save Timshel/6052496 to your computer and use it in GitHub Desktop.
Define a #play2 #form #constraint for #required #optional mapping
import play.api.data.validation._
trait Constraints {
/**
* Defines a ‘required’ constraint for `Optional` values, i.e. one in which None are invalid.
*
* '''name'''[constraint.required]
* '''error'''[error.required]
*/
def nonNone[A]: Constraint[Option[A]] = Constraint[Option[A]]("constraint.required") { o =>
if (o.isEmpty) Invalid(ValidationError("error.required")) else Valid
}
}
@julienrf
Copy link

Why use Option if you don’t want an optional value?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment