Skip to content

Instantly share code, notes, and snippets.

@mgladdish
mgladdish / WidgetWithMultipleConstraints.scala
Last active June 12, 2019 12:26
Shows an inputText with multiple constraints, each of which will be reported on the form
inputText("validationExample", "messages", required() ++ isNumber)
@mgladdish
mgladdish / WidgetWithOnlyOneReportedConstraint.scala
Last active June 12, 2019 12:26
Shows an inputText with multiple constraints, only the first of which will be reported on the form
inputText("validationExample", "messages", firstOnly(required(), isNumber))
section(
messages = "sectionText", // the name of properties bundle containing section-level text
nestingCheckboxes(
"myCheckboxes", // the unique name of our widget
"questionWording", // the name of the properties bundle containing text for the widget
required, // constraints applied to the widget
"contactMethods", // the name of the properties bundle containing the key and labels for each checkbox choice
Map( // our nested widgets
@mgladdish
mgladdish / contactMethods.properties
Last active June 12, 2018 17:17
Properties file for check box options
EMAIL=Email
PHONE=Phone
TEXT=Text message
def nestedInput[M <: Messages[M]](name: String, ...)(...) =
Input(name, ..., layoutTemplateName = "nestedWidgetBoilerplate.mustache", ...)
{{#choices}}
<div class="multiple-choice">
<input id="{{id}}_{{value}}" name="{{name}}" type="checkbox" value="{{value}}" {{#selected}} checked="checked" {{/selected}} />
<label for="{{id}}_{{value}}">{{{text}}}</label>
</div>
{{{nestedWidget}}} <!-- addition to output the html of the already-rendered nested widget -->
{{/choices}}
case class NestingCheckboxes[C <: Messages[C]](... nestedWidgets: Map[String, Widget]) extends NestingMultipleChoice[C] {
override val nestingMultipleChoiceWidget = new Checkbox(...) {
override def render(form: Form, errors: ValidationErrors, requestInfo: RequestInfo)(...): Future[String] = {
...
// pass result of templateChoices(form, errors, requestInfo) to my template and return the rendered result
}
}
}
case class NestingCheckboxes[C <: Messages[C]](... nestedWidgets: Map[String, Widget]) extends NestingMultipleChoice[C] {
override val nestingMultipleChoiceWidget = new Checkbox(...)
}
trait NestingMultipleChoice[C <: Messages[C]] extends WidgetGroup {
def nestedWidgets: Map[String, Widget]
def nestingMultipleChoiceWidget: MultipleChoice[C]
override lazy val widgets: Seq[Widget] = nestingMultpleChoiceWidget +: nestedWidgets.values.toSeq
def templateChoices(form: Form, errors: ValidationErrors, requestInfo: RequestInfo)(...): Future[List[Map[String, Any]]] = {
nestingMultipleChoiceWidget.choicesForTemplate.map(...
trait NestingMultipleChoice[C <: Messages[C]] extends WidgetGroup {
def nestedWidgets: Map[String, Widget]
def nestingMultipleChoiceWidget: MultipleChoice[C]
override lazy val widgets: Seq[Widget] = nestingMultpleChoiceWidget +: nestedWidgets.values.toSeq
def templateChoices(form: Form, errors: ValidationErrors, requestInfo: RequestInfo)(...): Future[List[Map[String, Any]]] = {
nestingMultipleChoiceWidget.choicesForTemplate.map(...