[#7086] CB Code Field Issue

6 years 1 month ago - 6 years 1 month ago #302944 by bascherz
[#7086] CB Code Field Issue was created by bascherz
I hope I am just doing something wrong. I have a Code Checkbox (Multiple) field (cb_emails) whose options I want to be progressively presented as values in other fields are entered. For each of the other fields that is not empty, I want another option added to this Code Checkbox (Multiple) field. If the value in one of those other fields is deleted, that option should disappear from cb_emails.

The cb_emails field code looks like this:
if ($user->cb_emaila != '') $options=array('Email A'=>'Email A');
if ($user->cb_emailb != '') $options=array('Email A'=>'Email A','Email B'=>'Email B');
if ($user->cb_emailc != '') $options=array('Email A'=>'Email A','Email B'=>'Email B','Email C'=>'Email C');
return $options;

The cb_emails field is setup with Ajax to update whenever either cb_emaila (Email A), cb_emailb (Email B ), or cb_emailc (Email C) changes, which as I understand it makes the above code run again if any one of those fields changes. Each of these is progressively dependent on the previous one. So you won't even see the cb_emailb field until you've entered something for cb_emaila, etc.

As I progressively add email addresses to each of these fields (A, then B, then C), the checkbox options progressively appear as expected for cb_emails. Works great while going in the forward direction (entering new values). If you enter all three addresses, cb_emails ultimately shows checkboxes for Email A, Email B, and Email C. Perfect. But once you've entered all three cb_emaila, cb_emailb, and cb_emailc and then you delete the entry for cb_emailc, you'd expect cb_emails to show checkboxes only for Email A and Email B based on the above code. But that's not what happens. The options disappear altogether.

The other thing I thought was kind of weird about this field type is I still have to have at least one static option declared. In this particular case it doesn't matter since I always want [email] to be part of the list also, so "Account Email" is the static value that's always first in the option list and that checkbox never disappears.

What am I doing wrong?

Thanks in advance,
Bruce

______________________
Bruce S - Vienna, VA

Please Log in to join the conversation.

6 years 1 month ago #302960 by krileon
Replied by krileon on topic CB Code Field Issue

But once you've entered all three cb_emaila, cb_emailb, and cb_emailc and then you delete the entry for cb_emailc, you'd expect cb_emails to show checkboxes only for Email A and Email B based on the above code. But that's not what happens. The options disappear altogether.

You're completely replacing the options array per if case. You need to progressively add to it instead. Example as follows.

$options				=	array();

if ( $user->cb_emaila != '' ) {
	$options['Email A']	=	'Email A';
}

if ( $user->cb_emailb != '' ) {
	$options['Email B']	=	'Email B';
}

if ( $user->cb_emailc != '' ) {
	$options['Email C']	=	'Email C';
}

return $options;

The other thing I thought was kind of weird about this field type is I still have to have at least one static option declared. In this particular case it doesn't matter since I always want to be part of the list also, so "Account Email" is the static value that's always first in the option list and that checkbox never disappears. What am I doing wrong? Thanks in advance, Bruce[email] to be part of the list also, so "Account Email" is the static value that's always first in the option list and that checkbox never disappears.

You can still use static options with CB Code Field and CB Query Field select fieldtypes. They will always appear first.


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.

6 years 1 month ago - 6 years 1 month ago #302963 by bascherz
Replied by bascherz on topic CB Code Field Issue
Believe it or not, your recommended method was my first implementation attempt. If I recall, that resulted in there only ever being one checkbox showing. But I’ll give it another try. Having the static options there is actually a good thing in this case since as I said I want the Account Email to always be the first option.

Thanks!

Update: I must have done something slightly different the first time because it worked perfectly this time.

CB Code Field is such a powerful plugin I don’t know how I ever lived without it! Excellent stuff!

Thanks again!

______________________
Bruce S - Vienna, VA
The following user(s) said Thank You: nant

Please Log in to join the conversation.

6 years 1 month ago - 6 years 1 month ago #302998 by bascherz
Replied by bascherz on topic CB Code Field Issue
Well, now there's a couple new issues. These may be a bit trickier. Maybe this is what I was seeing the first time I tried Krileon's code.

I have two Code Checkbox (Multiple) (codemulticheckbox) fields that are supposed to refresh using that exact same code based on the exact same field values changing (cb_emaila, cb_emailb, and cb_emailc). But only one of them is refreshing. Refresh seems to alternate between them as I try this repeatedly. It's as if only one code field can be dependent on a particular other field (changing) for Ajax refreshing. If I make the two code fields refresh on changes to different fields, both code fields refresh when they should. It's only when both of them depend on the same other fields changing that only one or the other does the Ajax refresh.

Additionally, when the new checkbox appears, if it is checked that doesn't stick after updating the profile. This more or less makes this not quite as cool as it would have been.

Hopefully I've stated that clearly enough.

To summarize how this is supposed to work:
1. The code field (call it cb_list1) is a codemulticheckbox type.
2. Three other text fields are associated with cb_list1: email address (cb_emaila), first name (cb_emailafirstname), and last name (cb_emailalastname). The code field is set to update via Ajax when cb_emaila is changed by the user.
3. For example, when the user enters an email address in the cb_emaila field, Ajax dependency on cb_emaila changing makes the cb_list1 code field's code execute. This makes the Email A checkbox appear.
4. Since the user has an address entered for Email A, it's now valid to check that checkbox and click the profile Update button. However, when that checkbox is checked, that is not saved to cb_list1 when the profile Update button is checked.
5. If I have multiple code fields, e.g., cb_list1 and cb_list2, that work the same way and are dependent on the same fields (cb_emaila, etc.), the code fields do not all update together; only one of them does. And the one that does seems to alternate through all the code fields that are Ajax-dependent on the same text fields.

Hopefully it's crystal clear now.

Things I've tried:
1. Having one cb_list code field update on cb_emaila and the other on cb_emailalastname. This makes the Ajax refreshes work better, but still not reliably. It's even less reliable if I use cb_emailafirstname for cb_list1 and cb_emailalastname for cb_list2, which I found somewhat strange but it might be a clue.
2. Using different array names in each field's code ($list1 and $list2 instead of just $options for both of them). This made no difference at all and I didn't think it would.

Scratching my head on this one...

______________________
Bruce S - Vienna, VA

Please Log in to join the conversation.

6 years 1 month ago #303006 by krileon
Replied by krileon on topic CB Code Field Issue
Don't use the Update On included with CB Code Field. Use the Update On functionality in CB Core Fields Ajax. The usage in CB Code Field and CB Query Field will be removed in their next major releases as CB Core Fields Ajax significantly improves on it and should work fine with multiple fields.


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.

6 years 1 month ago - 6 years 1 month ago #303031 by bascherz
Replied by bascherz on topic CB Code Field Issue
That's exactly what I've been using. It works as I described.


______________________
Bruce S - Vienna, VA

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.186 seconds

Facebook Twitter LinkedIn