Forum

Thread tagged as: Problem, Field-Types

Templates - default attribute fault

I am using a checkbox control to show/hide content with Perch.

eg.

<perch:if exists="show">
   content
</perch:if>

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

Unfortunately, the default attribute causes the box to be checked in Perch UI whenever I revisit the region, regardless of the state it should be. I.e. if I uncheck and save, the box ticks itself again.

Without the default attribute, the checkbox functions as expected.

Tom Turton

Tom Turton 0 points

  • 7 years ago

Perch v2.6

Drew McLellan

Drew McLellan 2638 points
Perch Support

The default attribute is used for a field whenever it's blank, so it doesn't really make sense for a checkbox.

Drew McLellan said:

The default attribute is used for a field whenever it's blank, so it doesn't really make sense for a checkbox.

Ok, is there a way of giving the checkbox a default value, without breaking it?

Drew McLellan

Drew McLellan 2638 points
Perch Support

How would that work, conceptually? I just don't think it makes sense with checkboxes.

By 'default value', I mean checked or unchecked.

Normally, when an editor creates a new item in the region, they'll want the content to show, so the box should be checked already. If they decide not to show the content they would uncheck the box. This is an opt-out situation and is most convenient for the editor.

In other regions, it might be preferential to have to opt-in to showing content instead. Certainly in my case this is less likely, but is how <perch:content type="checkbox" ... /> behaves without modification.

Drew McLellan

Drew McLellan 2638 points
Perch Support

All the checkbox knows is if it's checked or not. I has no way of seeing if it was checked in the past or anything like that.

I'm not sure you understand what I mean.

All I'm asking about is when a editor creates a new region item, can the checkbox be checked by default?. Just like the way <input type="checkbox" checked="checked"> behaves.

As the editor is far more likely to want it checked, it would be helpful if they wouldn't have to tick it themselves. Seems petty, but I don't feel it's professional if they have to tick the box for the vast majority of items they add, and it could get annoying.

See my original post on the simple content template I am trying to write for this situation.

Thanks.

Drew McLellan

Drew McLellan 2638 points
Perch Support

I understand.

  1. The page loads. The checkbox has no value set, so it uses the default of checked.
  2. The user unchecks the checkbox and saves. Unset fields have no value.
  3. The page loads. The checkbox has no value set, so it uses the default of checked.

That's the issue.

Yup, I understood from your first reply that default is now the wrong attribute to use here.

It doesn't sound like there is a way of doing this. Perhaps it's something you can look at for a future version?

In my situation, I think I will change the box's function to hide content if checked. Do I have to write this as:

<perch:if exists="hide">
<perch:else />
    content
</perch:if>

<perch:content type="checkbox" value="1" suppress="true" id="hide" label="Hide content" />

or is there a more direct way of asking if a field does not exist?

Thanks

Drew McLellan

Drew McLellan 2638 points
Perch Support

<perch:if id="hide" match="neq" value="1">
    ...
</perch:if>

Thank you