-
-
Save Timshel/6052496 to your computer and use it in GitHub Desktop.
Define a #play2 #form #constraint for #required #optional mapping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why use
Option
if you don’t want an optional value?