SHtml.checkbox removes id attribute from template

165 views
Skip to first unread message

Jakub Czuchnowski

unread,
Jan 10, 2013, 9:09:52 PM1/10/13
to Lift
Hi,

When I'm using SHtml.checkbox the id attribute of the original element
gets lost. So in simple transformation like:

"#checkboxField" #> SHtml.checkbox(checkboxVal, checkboxVal = _)

I get:
<input id="checkboxField">

turned into:
<input name="F958524152726VSM5A1" type="checkbox" value="true">

I noticed that SHtml.text and SHtml.select work differently - they
preserve the original id of the element. Is that on purpose or should
I use some other checkbox method?
Here's a very simple example where you can see the difference
https://github.com/jczuchnowski/lift-test

Thanks,
Jacob

Diego Medina

unread,
Jan 11, 2013, 4:03:22 PM1/11/13
to Lift
try

"#checkboxField" #> SHtml.checkbox(checkboxVal, checkboxVal = _,
("id", "checkboxField" ))

I can;t look right now, but most of the SHtml functions have a 3rd
parameter that adds attributes to the element we replace.


Regards,

Diego


On Thu, Jan 10, 2013 at 9:09 PM, Jakub Czuchnowski
<jakub.cz...@gmail.com> wrote:
> "#checkboxField" #> SHtml.checkbox(checkboxVal, checkboxVal = _)




--
Diego Medina
Lift/Scala Developer
di...@fmpwizard.com
http://www.fmpwizard.com

Antonio Salazar Cardozo

unread,
Jan 11, 2013, 4:21:51 PM1/11/13
to lif...@googlegroups.com
Yes, I think you have to use this. This is because most of the SHtml functions return an Elem. For these, Lift's selector transforms can merge the attributes in the original element into the attributes from the resulting element. However, for checkboxes, SHtml returns a NodeSeq: it returns both the checkbox input and a hidden input that has the value the checkbox should take if it is unchecked. This is because an unchecked checkbox is not submitted as part of a form, so you have to use a hidden checkbox with the same name to get a value submitted when the checkbox is unchecked.

Anyway, the result is that no attribute merging can take place, so you have to be explicit about the attributes you want to pass on.
Thanks,
Antonio

Jakub Czuchnowski

unread,
Jan 13, 2013, 8:16:55 AM1/13/13
to Lift
Thanks for the explanation. I know about the SHtml functions parameter
where I can pass the attribute. I was just hoping for something more
consistent for checkbox. But I get the Elem / NodeSeq problem.

Thanks,
Jacob

On Jan 11, 10:21 pm, Antonio Salazar Cardozo <savedfastc...@gmail.com>
wrote:
> > di...@fmpwizard.com <javascript:>
> >http://www.fmpwizard.com

Alexandre Richonnier

unread,
Sep 5, 2013, 7:11:47 PM9/5/13
to lif...@googlegroups.com
Hi,

When you say "Lift's selector transforms can merge the attributes in the original element into the attributes from the resulting element", How can we doing that?

e.g.:
In my template:
<input tabindex="1" class="cl1 cl2" data-show="true" id="id1"/>


code:
 "#cl1" #> SHtml.checkbox(v1, v1 = _, ("tabindex", "1"))



expected:
<input type="checkbox" value="false" name="F749261991525KPCLWJ" tabindex="1" class="cl1 cl2" data-show="true" id="id1">
<input type="hidden" value="false" name="F749261991525KPCLWJ">


but:
<input type="checkbox" value="false" name="F749261991525KPCLWJ" tabindex="1">
<input type="hidden" value="false" name="F749261991525KPCLWJ">



Need I do something like from https://groups.google.com/d/msg/liftweb/c0O7jV3srWM/0q-cU42uDLwJ :
 
 "remember_me" -> ((kids: NodeSeq) => {
           val atts
= BindHelpers.currentNode.
toList
.flatMap {
             
case e: Elem => e.attributes.map(m  => (m.key, m.value.text))
             
case _ => Nil

           
}
           
SHtml.checkbox(rememberMe, rememberMe = _, atts.toList: _*)

Or we have some new feature in lift 2.5.1 to do it?

Thanks,

Alexandre

Antonio Salazar Cardozo

unread,
Sep 6, 2013, 1:40:54 PM9/6/13
to lif...@googlegroups.com
Unfortunately attribute merging doesn't actually work for checkboxes. This is because SHtml.checkbox doesn't actually return a checkbox; instead, it returns a NodeSeq, because it also needs to insert the hidden input that indicates to the server that the value is present in the form submission, but not selected. This messes with Lift's internal smarts.

You can instead pass the attributes at the end of the call to SHtml.checkbox, with the BindHelpers thing being one example of that.
Thanks,
Antonio

ari gold

unread,
Dec 7, 2017, 10:43:33 PM12/7/17
to Lift
Almost 5 years later and that ("id", "field-name") trick worked like a charm!

:D
Reply all
Reply to author
Forward
0 new messages