[Resolved] How to limit the number of categories that can be assigned in a post edit form
This thread is resolved. Here is a description of the problem and solution.
Problem:
When adding/editing posts with a front-end form how to limit the number of categories that can be assigned? A select field allows only one, but checkboxes or multi-select don't have a way of limiting how many terms can be applied.
Solution:
There is no built-in support for this within Toolset.
Probably the best option would be to use a select dropdown and then add the select2 library where the maximum number of terms can be specified.
I have a Edit Form for a CPT and I need to include 3 categories inside (they are the same taxonomy with 3 different fields to select).
The form is working fine, (it saves and show the fields correctly) but when I try to edit again, those 3 taxonomy fields set automatically in the first of them).
-> I've prepared a serie of screenshot in order to explain better the milestone of the problem.
1. Edit form with the 3 fields. I select "Cascos históricos" / "Calles" / "Castillos"
2. Save the Edit form
3. The edit form shows the fields correctly.
4. I go again to the "Edit form"
5. -> PROBLEM // The fields have changed. Now they are "Cascos históricos" / "Cascos históricos" / "Cascos históricos"
I assume it's something related with those 3 same taxonomy field, because if I use only 1 field the form works as expected.
I assume also that a solution would be give the user the option of choose more than 1 category in the dropdowns, but I would prefer not to do that (I prefer 3 fields with the one-value option).
Thank you for waiting and I can access the website's admin area now.
( interestingly it works only in Chrome's incognito mode for me )
From the screenshots, it seems you were referring to "Editar contenido - Usuarios Guia produccion".
If that is correct, can you please share the link to a page where this form can be seen? I'd like to understand the exact workflow, to suggest something accordingly.
Actually the screenshots I send to you are not related with "Editar contenido - Usuarios Guia produccion" but with "Edición de localizaciones". But the problem it's the same in both Edit forms.
I updated your user in order you can access to both forms (if you need it).
So, you can go to your user profile here hidden link
You have your location here hidden link
You can edit one location here hidden link
You have your production here hidden link
You can edit it here hidden link
I hope this helps you.
If you need anything else from me, just let me know.
🙂
Waqar is on vacation this week so I'm stepping in to cover.
I tried the site credentials you provided to Waqar but was unable to log in, though it doesn't matter as I was able to reproduce what you are describing on my local test site.
And I would say that what you describe is entirely expected.
The post you are editing with your edit form has several taxonomy terms assigned.
When you include this taxonomy field in your form the options allow for displaying the taxonomy as checkboxes, a multi-select, or a single select.
Naturally when you choose single select it can only display a single term, and if there are multiple it will only show the first one.
If you include a single select field several times, each time it will show the first one.
If you want to display all terms you must choose either checkboxes or multi-select.
You could persist with your current implementation for the original post form, but not for the edit form.
Ok. I understood. So it's working as expected.
It's a pity because I would like to limit the number of selections to 3 in the taxonomy, and with "one select" option it seems better and nicer than "multiselect" for me.
Anyway, is there any way to limit the number of fields selected in a multi-select option? (I think it's the better solution)
Another option could be duplicate 3 times the taxonomie (so I will have 3 different taxonomies with the same fields to have the "one select" option working), but this may give problems with the search form and it doesn't seem like a 'clean' way to solve the problem right?
If the rationale for what you are doing is to limit the number of options that can be selected then you might want to try something like converting a multiselect dropdown into a select2 dropdown, when you can then specify in the options the maximum number of options that can be selected.
You would need to add the select2 assets to the page, as described here: hidden link
And then instantiate select2 for the dropdown with the setting for the maximum number of options as described here (where you can see an example): hidden link
I think that is the best option given your use case, do you want to try that?