Forum

Thread tagged as: Question, Problem

Bug or just illogical behaviour?

Hi, not sure if this is a bug or not?

I've got a template with a checkbox included, giving editors the option to show/hide a certain part of the page. By default it's ticked to show the section and I've done this using the default attribute (default="1").

But, when an editor un-ticks the box, it does save and hides the section - but the next time they visit that page to edit another part of it - it's checked again meaning the section is unknowingly re-published.

Is there a better way to achieve this other than default, or is it a bug?

Haydn Ward

Haydn Ward 4 points

  • 6 years ago
Rachel Andrew

Rachel Andrew 394 points
Perch Support

We need to see your template to advise.

The checkbox causing the problem is this line in the below template:

<perch:content id="show-labels" type="checkbox" label="Show sizing labels?" value="1" default="1" suppress="true"/>

Full template:

<section>   
    <header>    
        <h1><perch:content id="menupagetitle" type="text" label="Menu Page Title" required="true" title="true" order="1" /></h1>
        <perch:content id="slug" for="menupagetitle" type="slug" suppress="true" />
        <perch:content id="perch_item_first" type="hidden" suppress="true" />
    </header>

<perch:blocks>
<perch:block type="text" label="Menu Section">
    <article class="menu-section">
        <header class="menu-section-header">
            <img src="<perch:content type="image" id="header-image" label="Menu Section Image" crop="true" width="300px" height="150px" order="3" />" alt="" title="<perch:content id="menu-section-title" type="text" label="Menu Section Title" order="2"/>">
            <h2><perch:content id="menu-section-title" type="text" label="Menu Section Title" /></h2>
            <perch:content id="menu-section-description" type="textarea" label="Menu Section Description" html="true" size="xs" order="4" />
        </header>

        <perch:content id="show-labels" type="checkbox" label="Show sizing labels?" value="1" default="1" suppress="true"/>
        <perch:if exists="show-labels">
        <div class="item-size">
            <div class="item-small"><perch:content id="menu-label-small" type="text" label="Label 1" default="Small" /></div>
            <div class="item-large"><perch:content id="menu-label-large" type="text" label="Label 2" default="Large" /></div>
        </div>
        </perch:if>
<perch:repeater id="menu-items" scope-parent="true" label="Menu Items">
<perch:before>
    <div class="menu-items">
        <ul>
</perch:before>
            <perch:content id="item-special" type="checkbox" label="Special?" value="1" suppress="true"/>
            <perch:content id="veg-option" type="checkbox" label="Suitable for vegetarians?" value="1" suppress="true"/>
            <li <perch:if exists="item-special">class="special"</perch:if>>
                <div class="item-title">
                    <h4><perch:content id="menu-item-title" type="text" label="Menu Item" required="true" /><perch:if exists="veg-option"><img src="/images/icons/icon-menu-vegetarian.png" class="veg-icon" title="Suitable for vegetarians" alt=""></perch:if></h4>
                </div>
                <div class="item-costs">
                    <div class="item-costs-inner">
                    <div class="item-small"><perch:if exists="parent.show-labels"><span class="item-size-lable"><perch:if exists="parent.menu-label-small"><perch:content id="parent.menu-label-small" type="text" />: </perch:if></span></perch:if><perch:if exists="menu-item-cost-small"><perch:content id="menu-item-cost-small" type="text" label="Price - Small" /><perch:else />&nbsp;</perch:if></div>
                    <div class="item-large"><span class="item-size-lable"><perch:content id="parent.menu-label-large" type="text" label="Label 2" default="Large" />: </span><perch:content id="menu-item-cost-standard" type="text" label="Price - Standard/Large" required="true" /></div>
                    </div>
                </div>
                <div class="item-description">
                    <perch:content id="menu-item-description" type="textarea" label="Menu Item Description (optional)" html="true" size="xs" />
                </div>
                <div class="clear"></div>
            </li>
<perch:after>
        </ul>
    </div>
</perch:after>
</perch:repeater>
    </article>
</perch:block>
</perch:blocks>

    <div class="clear"></div>
</section>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Checkboxes are deceptively complex. They only have a value when they're checked - there's no negative value. So when repopulating the field, if the value attribute matches what we have stored for the checkbox, it gets checked.

If there's no value set, we use the default, as you'd expect.

The issue comes when you've unchecked the box - it has no value - so we use the default. That's what you're seeing.

A workaround might be to use a Yes/No select box.

Thanks Drew

I kinda guessed it would be down to the genuine awkwardness that is the ever popular checkbox!

A yes/no select seems like a relatively straightforward replacement.

Thanks :)