Scala – How to hide a text field in play framework


How to hide a text field in play framework? For example how to hide this field:

@inputText(userProfileForm("name"), '_label -> "Name")

Best Solution

This should work in all browsers:

    '_label -> "Name",
    'style -> "display: none"

Note that this only hides the field, not the label etc.

If you want to hide the label aswell, you can't do this with the default helpers. You can however specify the input field yourself:

<input type="hidden" name="name" value=""name")" />

The name should be name of the field in your form (coincidentally name aswell in this case).
I haven't tested the value but it should work, maybe you'll need to strip the " around name.

If you do this, the hidden data will be sent, along with the other data in the form to the server.

If the hidden value is empty, that means you didn't bind it to the form when calling the view. You can bind the name to the form like this in Java (I don't know Scala, but it's explained in the Play Scala documentation):

Map<String, String> data = new HashMap<>();
data.put("name","get the username here");

return ok(index.render(userProfileForm.bind(data));

Another option (which is cleaner in my opinion) is to simply pass the username as an argument to the view. The controller becomes:

return ok(index.render(userProfileForm, "username goes here"));

And in the view you can then simply do:

@(userProfileForm : Form[UserProfileForm])(name : String)

    <input type="hidden" name="name" value="@name" />