Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Improvement: make it easier to pass HTML 5 data attributes to form inputs #2061

Closed
martinburger opened this Issue Nov 20, 2013 · 5 comments

Comments

Projects
None yet
3 participants

Currently, it is not easily possible to pass HTML 5 data- attributes to form inputs. As case class FieldElements uses a map of type Map[Symbol, Any], one has to provide instances of class Symbol. For instance:

@inputText(someform("somefield"), '_label -> "some label")

While this way, it is very convenient to pass additional arguments like label using the apostrophe, it is not easily possible to pass a data- attribute, like:

@inputText(someform("somefield"), '_label -> "some label", 'data-foo -> "bar")

The above code would result in a compilation error "value - is not a member of Symbol" as Scala interprets the dash and the hyphen-minus sign, respectively, as a minus sign.

At Stack Overflow, at least two people stumbled upon the above issue:

The first question results in the answer to explicitly calling Symbol("data-foo") which would make the view very verbose. The second question results in the answer to define an implicit that would do the conversion.

I would propose some implicit value class like the following:

object SymbolHelper {

  implicit class SymbolHelper(val str: String) extends AnyVal {
    def s = Symbol(str)
  }

}

Then, one could define the field as follows:

@inputText(someform("somefield"), '_label -> "some label", "data-foo".s -> "bar")
Owner

jroper commented Nov 20, 2013

If we're going to add an implicit conversion, why not just provide:

implicit def toAttributePair(pair: (String, String)): (Symbol, String) = (Symbol(pair._1), pair._2)
Owner

gmethvin commented Nov 22, 2013

I agree. More generally, we could define something like:

implicit def toAttributePair[B](pair: (String, B)): (Symbol, B) = Symbol(pair._1) -> pair._2
Owner

jroper commented Nov 22, 2013

I'm more inclined to be as limiting as possible with the scope - implicit conversions are not things that you want to be floating around for general use.

Owner

gmethvin commented Nov 22, 2013

Sure. I suppose for HTML attributes you'll only really need strings anyway.

@jroper jroper added a commit that referenced this issue Nov 25, 2013

@jroper jroper Merge pull request #2078 from gmethvin/attribute_pair
[#2061] Add implicit conversion for string attribute pairs
50bc8d3
Owner

jroper commented Nov 25, 2013

Fixed in #2078.

@jroper jroper closed this Apr 8, 2014

@mkurz mkurz referenced this issue in adrianhurt/play-bootstrap Nov 26, 2014

Closed

Document how to add attributes with dashes (e.g. data-*) #12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment