Search

I got a front-end form and having trouble with checking a checkbox by default.
I was thinking about achieving this with a condition:

<xsl:choose>
        <xsl:when test="events/*/post-values/checkbox ='on'"><xsl:attribute name="checked">checked</xsl:attribute></xsl:when>
        <xsl:otherwise><xsl:attribute name="checked">checked</xsl:attribute></xsl:otherwise>
</xsl:choose>

What this would do is have the checkbox checked by default: (the otherwise statement, cause there would be no post values)
When sending the form checked, the post-value returns 'on' so it returns the checked state.

Now here's the problem: there's no post value for unchecked checkboxes, like this:

<xsl:when test="events/*/post-values/checkbox ='off'"></xsl:when>

This would return an unchecked checkbox when it's not checked.

So my question; why is there no post-value data for unchecked checkboxes?
And are there any other solutions to get this working? i am kinda stuck here…

Cheers

This is just how unchecked checkboxes in HTML are handled. If they're not checked, there is no value to POST.

I'm not sure, but can you do something like:

<xsl:when test="not(events/*/post-values/checkbox ='on')"></xsl:when>

When you are using checkboxes in Symphony, it is often wise to add a hidden field above the checkbox with the same name as the checkbox, with a value of no.

If the checkbox is checked, the hidden field will not be submitted, but if the checkbox is unchecked, it will.

This makes form handling a whole lot easier (especially with custom events).

Adding the hidden field did the trick,
It returns the post-value 'no' when the checkbox is unchecked

Then using this condition gives the desired result:

<xsl:choose>
        <xsl:when test="events/*/post-values/checkbox ='no'"></xsl:when>
        <xsl:otherwise><xsl:attribute name="checked">checked</xsl:attribute></xsl:otherwise>
</xsl:choose>

Thanks guys!

Cremol, what does your hidden field look like?

The simplest things are proving a head ache today :)

@moonoo: It looked like this...:

<input name="fields[checkbox]" type="hidden" value="no"/>
        <input name="fields[checkbox]" type="checkbox">
        <xsl:choose>
            <xsl:when test="events/*/post-values/checkbox ='no'"></xsl:when>
            <xsl:otherwise><xsl:attribute name="checked">checked</xsl:attribute></xsl:otherwise>
        </xsl:choose>
        </input>

Nice waaaan!

Thanks Martijn! you good :)

No worries!

This post saved me the headache, and it s the solution if you want to display the checkbox checked by default, here is what I use if you want to have the checkbox unchecked:

<input name="fields[checkbox]" type="hidden" value="no"/> 
<input name="fields[checkbox]"  type="checkbox">
<xsl:if test="events/*/post-values/checkbox ='yes'"><xsl:attribute name="checked">checked</xsl:attribute></xsl:if>
</input>

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details