0

I hava a template that creates a checkbox and a text input field:

@helper.checkbox(inputForm("live"), 'checked -> true, 'onclick -> "hideTextFields()")
@helper.inputText(inputForm("target"), 'disabled -> true)

When unchecking the checkbox, the text fields get visible (and vice versa). This is done in a javascript function called hideTextFields().

Now, in the controller I need to have a constraint, that makes the text input field mandatory as soon as the checkbox is unchecked.

I have a mapping:

"live" -> boolean
"target" -> nonEmptyText

but this always requires the target to be specified.

How can I achieve dynamic constraints for the text input field based on the checkbox's state. Play is version 2.3.4.

Thank you and best regards!

1
  • Stupid tag... choose better
    – biesior
    Commented Nov 2, 2014 at 0:07

1 Answer 1

0

Check out the verifying methods that are on Mapping, which you get back from tuple() and mapping()

Simple example:

val sample = Form(
  tuple(
    "checkbox" -> boolean,
    "text" -> text
  ).verifying(
    "If checkbox checked text cannot be empty", 
    (tuple) => !tuple._1 || tuple._2.nonEmpty)
  )

You can find another example under "Defining ad-hoc constraints" in the play framework docs: The playframework docs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.