How to hide a text field in play framework? For example how to hide this field:
@inputText(userProfileForm("name"), '_label -> "Name")
This should work in all browsers:
@inputText(
userProfileForm("name"),
'_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="@userProfileForm.data.get("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.
Edit:
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)
@helper.form(...){
<input type="hidden" name="name" value="@name" />
//...
}
The inputText
helper takes a varargs of (Symbol, Any)
, which represent the html attributes. If you are using html5 you can just add the hidden attribute:
@inputText(userProfileForm("name"), '_label -> "Name", 'hidden -> "hidden")
normally the hidden
attribute has no value, but I couldn't find any information about how to do this with the helpers. In Chrome at least it works like this as well.
edit:
btw, you can also just use html instead of the helper:
<input attribute=value hidden />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With