Skip to Content Skip to Menu

🎃 Happy Halloween! Treat yourself with an awesome discount on memberships! Get 20% off now with code SPOOKY-2024!

Limiting number of multi-select (query) options possible

  • cpaschen
  • cpaschen
  • OFFLINE
  • Posts: 328
  • Thanks: 42
  • Karma: 9
8 years 1 month ago #285446 by cpaschen
I need to limit the number of options that a user can selecting a query - drop-down multi select field.

In:
www.joomlapolis.com/forum/87-presales/234523-hide-options-in-a-cb-query-based-on-selected-options-in-a-previoius-cb-query

Nant mentioined:

Not with CB Query by itself and not dynamically.
You could use a CB Auto Action to check if more than 3 options were selected and then either keep the first three selected or display some error message.

Is there any trigger that would be 'onAfterFieldChange' (or something like that)? (so we could trigger some code that counts the values selected as they select)?
or
Does the 'onBeforeUserProfielSaveRequest' happen before when the user clicks the update button from a profile edit? (so we could do the validation before the data is saved)

Also ... do you have a list of all the Triggers, explaining exactly when they are called? (Some of the names I'm not sure when they happen by their title)

Please Log in or Create an account to join the conversation.

  • cpaschen
  • cpaschen
  • OFFLINE
  • Posts: 328
  • Thanks: 42
  • Karma: 9
8 years 1 month ago #285447 by cpaschen
Also, do you have a 'getting started with Auto Actions' document or video?
I couldn't find one that gives a good introduction to AutoActions (just a few use-case examples that don't really explain how everything works)

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48456
  • Thanks: 8280
  • Karma: 1443
8 years 1 month ago #285478 by krileon
We don't have any functionality in CB to limit the selection. A custom validation plugin would be best case, but jQuery can be used to limit the select. This however won't apply any restrictions on the PHP validation. A multi-select is stored as |*| delimitered. So a code action can be used to explode it and count the results and that's how many are selected or you could count the occurrences of |*| then add 1.

Also ... do you have a list of all the Triggers, explaining exactly when they are called? (Some of the names I'm not sure when they happen by their title)

No, we've only the reference below. Triggers are typically named obvious to what they do. Some of the older triggers in core CB may not be as obvious, but all newer triggers should be. With that said when in doubt simply ask as we're here to help.

www.joomlapolis.com/documentation/279-community-builder/tutorials/18358-using-cb-triggers

Also, do you have a 'getting started with Auto Actions' document or video?
I couldn't find one that gives a good introduction to AutoActions (just a few use-case examples that don't really explain how everything works)

We have the below videos with various usage examples.

www.youtube.com/playlist?list=PLp0puRITgC7McQcw0Sli1qfSUQGwmflzi


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

  • cpaschen
  • cpaschen
  • OFFLINE
  • Posts: 328
  • Thanks: 42
  • Karma: 9
8 years 1 month ago - 8 years 1 month ago #285505 by cpaschen
Thanks for the tips.
(Not sure HOW I missed those videos).

Also, for anyone reading this in the future, this is how I managed to implement the selection restriction.

For the (query multi) field that needs to be limited:

Create a INTEGRATIONS | CB CODE FIELD validation (enable the validation).

Then use the following code:
Code:
$originalresults = $value; $noasterisk = preg_replace('/[\*]+/', ',', $originalresults); $csvlist = preg_replace('/[\|]+/', '', $noasterisk); $answers = explode(",", $csvlist); $count = count($answers); if ($count < 4) { $result = true; } else { $result = false; } return $result;

This works in multiple fields (i.e. you can just paste this code into multiple query fields and it will work with all.
You can change the number in the 4th line (i.e. "4") to one more than you want to have the limit set (limit of 5 - set to 6, etc.)

NOTE: This does NOT provide a live validation. Although I tried using the "Ajax Validation", it didn't have any effect (probably because I only have PHP code here).

If someone is able to improve this code (or make it function with real ajax validation) please share your solution.

Thanks again to the CB team. I couldn't have done this with any other extension. (And believe me, we tried :-( )
Last edit: 8 years 1 month ago by cpaschen.
The following user(s) said Thank You: krileon

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48456
  • Thanks: 8280
  • Karma: 1443
8 years 1 month ago #285528 by krileon
The ajax usage should work. Does it just do a swirl animation icon then nothing? Ajax validation should only trigger if the value has changed from its previous value as well. You can use $value (also have access to $user and $field) or [value] to access the value the user supplied.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

Moderators: beatnantkrileon
Powered by Kunena Forum