[#5840] Limiting the number of selected items in a multiple checkbox field

5 years 3 months ago - 5 years 3 months ago #279051 by peli_m
Hello,

I'm trying to limit to 2 the number of elements selected in a multiple checkbox field, using Ajax in frontend by the users.

In Integrations > CB Code Field > Code Validation Active, Ajax Validation Active, I have added this code:
$val = $field->value;
if ( substr_count ($val, '|*|' ) > 1) 
return false;
else return true;

Nothing happens. Changing $val = [value]; or if ( substr_count ($val, ',' ) > 1) ... the same, nothing happens.

Please, could you show me the way to do it?

Thank you!

Please Log in to join the conversation.

5 years 3 months ago - 5 years 3 months ago #279071 by krileon
There is no $field->value usage. The value is stored in the user object, but during validation nothing is stored. You strictly have to use $value or [value], but you need to treat it as a string. So for example you'd have the following.

return ( substr_count( '[value]', '|*|' ) > 1 );

Looks like there could be a bug though as it's missing its implode in the case the value is an array. So for now the below will probably work (should be backwards compatible as well once bug is fixed).

if ( ! is_array( $value ) ) {
    $value = explode( '|*|', $value );
}

return ( count( $value ) > 1 );

forge.joomlapolis.com/issues/5840


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 to join the conversation.

5 years 3 months ago #279076 by peli_m
Hello,

Thank you for your answer. I apologize but I don't get it to work

Please Log in to join the conversation.

5 years 3 months ago #279079 by krileon
Update to now released CB Code Field 1.0.3 then use the below usage. This should work fine.

if ( ! is_array( $value ) ) {
    $value = explode( '|*|', $value );
}
 
return ( count( $value ) > 1 ? false : true );


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 to join the conversation.

5 years 3 months ago #279081 by peli_m
Sure I have something wrong. I have updated to the new version 1.03 and add the code you tell, and nothing. I have change the all the code to only

return false;

And nothing happens, no error has reported and the selection in the field is written

Please Log in to join the conversation.

5 years 3 months ago #279110 by krileon
Ensure CB Code Field within CB > Plugin Management is public and published. It's possible to configure the parameters while the plugin is unpublished.


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 to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.492 seconds

Facebook Twitter LinkedIn