Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play 2.0 generate hidden field without div wrapper

Am generating a hidden fields like so:

@helper.input(_form("userID"), '_label-> None) { (id, name, value, args) =>
  <input type="hidden" name="@name" id="@id" value="@value" @toHtmlArgs(args)>
}

Which works fine except for the fact that the div wrapper block element is created, which creates visual empty space in the form, not looking good -- how to make hidden input elements display without div wrapper?

I know I can jQuery hide the parent div wrapper, but I'd like to not generate it in the first place.

like image 462
virtualeyes Avatar asked Nov 30 '22 05:11

virtualeyes


2 Answers

You don't need to use a @helper.input in this case. Try this:

@defining(_form("userID")) { uidField =>
  <input type="hidden" name="@uidField.name" id="@uidField.id" value="@uidField.value">
}
like image 143
Steven Luu Avatar answered Dec 10 '22 21:12

Steven Luu


Although the question has already been answered, I'd like to give a complete example because this would have helped me alot.

You may define your own hidden input helper in a file called "app/views/helper/inputHidden.scala.html" that would look like:

@**
* Generate a hidden HTML input.
*
* Example:
* {{{
*   @inputHidden(field = myForm("name"), args = 'data-ref -> 0)
* }}}
*
* @param field The form field.
* @param args Set of extra attributes.
* @param handler The field constructor.
*@
@(field: play.api.data.Field, args: (Symbol,Any)*)(implicit handler: FieldConstructor, lang: play.api.i18n.Lang)

@id = @{ args.toMap.get('id).map(_.toString).getOrElse(field.id) }
@inputType = @{ args.toMap.get('type).map(_.toString).getOrElse("hidden") }
@htmlArgs = @{ args.filter(arg => !arg._1.name.startsWith("_") && arg._1 != 'id).toMap.filter(_._1 != 'type) }

<input type="@inputType" id="@id" name="@field.name" value="@field.value" @toHtmlArgs(htmlArgs)>

This allows you to reuse the code in all your views and to give additional parameters like data-ref="..." to your hidden field.

like image 22
teemoo Avatar answered Dec 10 '22 23:12

teemoo