Forum

Thread tagged as: Question, Problem, Discussion

Select Box with Items That Have a Comma

Hi. So I'd like to give my client the option to select a date in a list that has a comma in it. The selected result will look like:

Monday, Sept 24

So in the perch:content, here's the issue:

<perch:content id="day-date" type="select" label="Day and Date"
options="Monday, Sept 24, Tuesday, Sept 25, Wednesday, Sept 26" allowempty="false" required>

Of course the comma in "Monday, Sept 24" is splitting this into two items. "Monday" and "Sept 24".

So is there a way to include a comma in this syntax without making two different selects?

Leigh C

Leigh C 0 points

  • 3 years ago

yes there is... \,

Escape the comma with a backslash...

Not sure about escaping commas in this. Not very compact but I think this will work

<perch:content id="day_date" type="select" label="Day and Date" options="Monday Sept 24 |mon_sept_24, Tuesday Sept 25|tue_sept_25, Wednesday Sept 26|wed_sept_26" allowempty="false" required suppress>

<perch:if id="day_date" value="mon_sept_24">
    Monday, Sept 24
</perch:if>

<perch:if id="day_date" value="tue_sept_25">
    Tuesday, Sept 25
</perch:if>

<perch:if id="day_date" value="wed_sept_26">
    Wednesday, Sept 26
</perch:if>

Wow... really Greg, I am not sure it can get easier then

<perch:content id="day-date" type="select" label="Day and Date"
options="Monday\, Sept 24, Tuesday\, Sept 25, Wednesday\, Sept 26" allowempty="false" required>
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Leigh,

Escaping commas works for public facing forms on the site (i.e. using perch:input), but doesn't work for fields in the control panel.

So Robert's approach works for perch:input in forms on the website. Greg's approach works for perch:content (and other namespaces) in the control panel and Drew suggested the same approach here:

https://forum.grabaperch.com/forum/01-19-2015-how-to-have-a-list-box-in-the-cms


Should you use either approach? I don't know what your use case is, but if you are dealing with dates, I would use the date field type instead:

https://docs.grabaperch.com/templates/field-types/date/

The format attribute allows you to format the output however you want it. Plus the template won't be outdated (you wouldn't have to update the template after Sept 26).


p.s. You shouldn't use hyphens in your ID id="day-date" as they are not allowed. You can use underscores instead id="day_date".

Leigh C

Leigh C 0 points

Thanks for the help guys. Unfortunately, Greg's method didn't work, and Robert's doesn't apply for this case. I'm not using the date picker before the format of the date is just 4 days for an event. To reduce user error, I wanted to combine the dropdown. But backslashes don't work, the IF statements shown above don't work either. :(

Did you change the id to id="day_date" not id="day-date" ? The IF statements are working on my test setup.

Leigh C

Leigh C 0 points

Yes I did. Oddly it's showing nothing.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

If you change the ID, you need to save the edit form again.

Leigh C

Leigh C 0 points

Yep, I know.

<perch:content id="day_date" type="select" label="Day and Date" order="3" options=",Monday Sept 24|mon_sept_24, Tuesday Sept 25|tue_sept_25, Wednesday Sept 26|wed_sept_26, Thursday Sept 27|thur_sept_27" allowempty="true" help="Choose a date and it will be formatted properly. For example: Mon, Sep 24" suppress /><perch:if id="day_date" value="mon_sept_24">Mon, Sep 24</perch:if><perch:if id="day_date" value="tue_sept_25">Tue, Sep 25</perch:if><perch:if id="day_date" value="wed_sept_26">Wed, Sep 26</perch:if><perch:if id="day_date" value="thur_sept_27">Thur, Sep 27</perch:if>

I tried changing the ID's the non-matching ones, but that also did not work. Bazaar - I do this all the time and I don't see what's missing here.

Your above template is working fine for me.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

What does <perch:showall> display?