0

I am (again) struggling with how to keep the value in a Channel Form when showing category as checkboxes. There are quite a few posts here but I seem not to find the answer. Please, who can help me here with a piece of code that really works. This is my code, this does NOT keep the value.

{categories show_group="5"}

    <label class="checkbox">{category_name}
    <input type="checkbox" id="{category_id}" name="{category_id}[]" value="{category_id}"{checked}>
    </label>

  {/categories}

Thanks, and a Happy New Year

0

I am not sure what you mean with "keep the value" - The input type checkbox can have the attribute "checked" (resulting in a tick being displayed in the checkbox) - the value property does not set the tick.

When you run the above code of yours and look at the output - what does EE put there? Where does "{checked}" come from?

If you want to have the checkbox in checked status you will have to include some sort of IF statement:

<input type="checkbox" id="{category_id}" name="{category_id}[]" value="{category_id}" {if condition}checked{/if} />

===(Updated after comment)====

If you want to allow the user to edit the Channel Entry <--> Category relation without giving them full access to the Control Panel there are two approaches:

1.) Use the Channel Form (ex SafeCracker) https://docs.expressionengine.com/latest/channel/channel_form/#categories

2.) Customize the CP for your users: https://devot-ee.com/add-ons/zoo-flexible-admin

  • This is the code I put in. Chennel Form. Out comes a list of categories as checkboxes. The client can tick a checkbox, but after sending the form the value is not accepted by the database. (When opening the same form=entry again for editing that checkbox is empty. (In the Control Panel everything works, but I do not want to give the client access to the CP therefore this Channel Form. – Hansl Dec 31 '15 at 8:42
  • I was looking for a 'standard' solution for categories as checkboxes in stead of dropdown list, but I cannot find it anywhere. I do hope for a working solution of someone that did the same before. Many thanks! – Hansl Dec 31 '15 at 8:46
  • Oh, now I think I get what you want to do: You don't want to display the categories, but also give the user the ability to check/uncheck them and by doing so link/unlink the channel entry to a category - Is that correct? – Steven M Dec 31 '15 at 16:58
  • Yes. Channel Form, creating a new entry or editing an existing one. Category: in stead of the 'standard' dropdown a checkbox. The code I use to apply for field checkboxes does not work for categories and I wonder why. And I want to know if there is a possibility to use checkboxes for categories in Channel Forms. – Hansl Jan 1 '16 at 9:49
  • Commenting your answer: 1) the example shows only a dropdown 2) I do not want to give users acces to the CP, all the editing for users is on the frontend ( for several reasons) . I hope you know how to code categories as checkbox in a Channel Form? And keep the value after submit? Thanks for answering. – Hansl Jan 1 '16 at 9:56
0

Now that I understand what you are trying to achieve - Did you look at this Q&A?

Outputting categories as checkboxes - exp:channel:form / safecracker

  • Yes I did, even before I asked my question here. – Hansl Jan 3 '16 at 13:36
0

Finally got it right.

<legend>Välj en grupp</legend>
      {categories group_id="5"}
        <label class="checkbox" for="{category_name}">{category_name}
        <input type="checkbox" name="category[]" id="{category_name}" value="{category_id}"{checked}>
        </label>
      {/categories}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.