0

I am trying to build an input helper for Play Framework in Scala.

I would like to have my inputs of type radio and checkbox to be printed differently than the standard text or password input types ?

Here is my custom helper:

@(elements: helper.FieldElements)

<div class="form-group">
    <label for="@elements.id" class="col-lg-2 control-label">@elements.label</label>
    <div class="col-lg-10">
        <input type="text" class="form-control" id="@elements.id" />
    </div>
</div>

But this prints an text input instead of checkbox input. Which is strange behaviour.

I've read the documentation, but couldn't find anything about it.

Here is how I import this helper to view:

@implicitFieldConstructor = @{ FieldConstructor(generic.views.html.FormHelpers.twitterBootstrapInputSupraClub.render) }
@checkboxFieldConstructor = @{ FieldConstructor(generic.views.html.FormHelpers.twitterBootstrapInputSupraClubCheckbox.render) }

And this is how I call helpers to build input (generates inputs) in view:

@inputText(
    accountRegistrationForm("email"),
    '_label -> "email"
)

@inputPassword(
    accountRegistrationForm("password"),
    '_label -> password
)

@inputRadioGroup(
    accountRegistrationForm("regulationAcceptance"),
    options = Seq("true"->"Yes"),
    '_label -> "I agree with regulation",
    'type -> "radio"
)(handler = checkboxFieldConstructor, implicitly[Lang])

1 Answer 1

1

I might be missing something here but as far as I can see, you seem to me missing the right input type.

In your helper, try changing <input type="text"... to <input type="radio"...

You can pass the input type dynamically, too. e.g.

@(elements: helper.FieldElements, inputType: String)

<div class="form-group">
    <label for="@elements.id" class="col-lg-2 control-label">@elements.label</label>
    <div class="col-lg-10">
        <input type="@inputType" class="form-control" id="@elements.id" />
    </div>
</div>

EDIT:

I have something similar to this for radio buttons:

given that my userDetailForm contains canLogOn:

"company" -> nonEmptyText,
"canLogon" -> boolean,
"canTrade" -> boolean,

I created a function in views: radioSet(fieldName: String)

@radioSet(fieldName: String) = {
   <label for="@fieldName">Can Logon</label>
   <input name=@fieldName id=@fieldName type="radio" value = "true"  
   @if(userDetailForm(fieldName).value.getOrElse("false").equals("true"))
   {checked}>
}

Then I call it when I need it:

@radioSet("canLogon")

And I get: https://i.sstatic.net/QXslf.jpg (sorry I cant post images yet)

Sign up to request clarification or add additional context in comments.

4 Comments

Can You give me an example of @inputText(accountRegistrationForm("email"), '_label -> "email") usage with Your example?
What is your helper called, and how are you calling it?
Ok. I am asking how you are calling your custom helper as I can only see @inputText, @inputPassword and @inputRadioGroup which are all members of Play's helper package. I can't see how you are calling your custom helper. In the Edit, I have demonstrated what I do when I need a custom helper, which is declaring a new method, and then calling it when it's needed.
Ok. Now I see. I've added handler to post. As You can see I have other approach.

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.