ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Safecracker multi-checkboxes

October 09, 2012 6:13am

Subscribe [2]
  • #1 / Oct 09, 2012 6:13am

    Heiko

    135 posts

    This question may be related to a resolved thread.

    Hi there,

    I just had to realize that I’m still struggling with checkboxes in Safecracker edit-forms, although I thought I fixed the original problem with conditionals like this:

    <label class="checkbox">
    {if my_field == 'on'}
      <input class="input checkbox" type="checkbox" name="my_field" value="" {my_field} checked> On!
    {if:else}
      <input class="input checkbox" type="checkbox" name="my_field" value="on" {my_field}> On?
    {/if}
    </label>

    Now, this works well and all, but I’m ending up with a serious amount of if-statements, especially when using checkboxes with multiple options.

    So, how can I do it the right way?

    And btw, sorry Shane for confusing you with Dan in the original thread! 😉

  • #2 / Oct 10, 2012 4:47pm

    Kevin Smith

    4784 posts

    Hi Heiko,

    No worries! I’m sure Shane doesn’t mind.

    Could we step back a second? I looked at the previous thread, and I didn’t see anywhere that you laid out what this particular field in the form is for. What about the normal checkbox fields in SafeCracker is broken that your method above is trying to fix?

  • #3 / Oct 11, 2012 3:45am

    Heiko

    135 posts

    It’s a native checkbox field with two or more options, like for example “Do you want to subscribe to our newsletter?” (yes/no)

    The thing that is not working - or at least not as I would expect it to be - is showing the correct current state, when editing an entry via Safecracker.

    So for example, if I set the checkbox to yes, it will be saved with the entry, but it doesn’t show “Yes” in the edit-form, unless I put conditionals in there.

    Maybe it’s just supposed to work like this? Seems like a hassle.

    Thanks for your help!

  • #4 / Oct 12, 2012 6:15pm

    Kevin Smith

    4784 posts

    Oh, I see what you mean now. You’ll want to use the {custom_fields} variable pair here to give you more control over this. The docs talk about the custom fields loop this way:

    If using a field with options, such as Checkboxes or Dropdown, you can display the options in a loop, to give you more control over the markup. You have the four following sub-variables: {option_value}, {option_name}, {selected} and {checked}.

    Regarding this field alone, you could do this within your SafeCracker tags:

    {custom_fields}
            {if checkbox}
                    {options}
                            <label class="checkbox">{option_value}
                                    <input type="checkbox" id="{field_name}" name="{field_name}[]" value="{option_value}"{checked}>
                            </label>
                    {/options}
            {/if}
    {/custom_fields}

    Does that help?

  • #5 / Oct 13, 2012 3:30am

    Heiko

    135 posts

    Aaaah, I wasn’t really aware of the benefits of using the custom fields-loop for this situation, since I’ve been working without it all the time.

    I’m going to try this.

    Thank you!

  • #6 / Oct 15, 2012 8:58pm

    Robin Sowell

    13255 posts

    😉  The loop is handy!  Glad Kevin got you on the right track.  Let us know if you run into trouble with it.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases