0

This is a weird situation I admit.

I'm using Channel Forms to make all entries on my site created & editable via the front-end.

I have a category group of locations.

I use Primary Category to allow the user to specify what their primary location is. Some paid premium entries may also be assigned to additional categories (locations).

In the CP, when you specify a category as the Primary Category, it automatically selects that category even if it was previously unselected. This bahaviour is not carried over to Channel Forms. If I select a Primary Category via the front-end form the category is not actually selected. I use javascript on form submit to copy the Primary Category ID to a hidden category[] field to be sure that category is selected. This works fine on new entry creation.

The complication arises on entry edit via Channel Forms.

Now, if I edit the Primary Cateogry, I can use the same javascript to assign that cat ID as the value of a hidden field, but how do I deselect the old Primary Category? If I don't deselect the old category when Primary Category is changed, an author could add their entry to more and more locations if they continue to edit the Primary Category, when in most cases I want to limit them to having just the one Primary category selected.

So in essence, I need a hidden form field with a value equivalent to something like this:

<input type="hidden" name="category[]" value="not {some-cat-id}" />

Or another creative solution to use Channel Form (edit entry) to deselect old Primary Category when new one is selected.

  • 1
    I'd think that just setting the hidden field to the new category would erase the old one. If not, have you tried using a select instead, exactly like the one in the channel forms example (ellislab.com/expressionengine/user-guide/modules/channel/…) and just deselect/select the appropriate categories with javascript? – aelvan Feb 20 '14 at 22:01
  • 1
    The category field can have multiple options selected so it's defined as an array name="catagory[]", which means if you add a new value to that I don't think it removes other existing values. I am currently using a select box for the primary_category field, which functions fine on its own but has no connection to the category[] fields. What I need to do is hook it up with the category[] field to remove the "old" primary category when a new one is selected. This may work with a multi-select box and JS, but in my case I need to use checkboxes for additional category selection. – Benek Lisefski Feb 20 '14 at 22:16
  • I've found a solution that works for me based on a tip from an answer that has since been deleted by its author. If anyone wants to take a stab at answering this I still have a bounty to give out! – Benek Lisefski Feb 22 '14 at 7:13

Your Answer

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

Browse other questions tagged or ask your own question.