• Resolved sadeepa17

    (@sadeepa17)


    I have a custom form using cmb2. User submit information which gets then stored on related Events Calendar Pro plugin fields. All works fine and gets stored fine but the selceted values of a multicheck.
    I’m doing this

    $cmb->add_field( array(
    		'name'    => __( 'Special Acts', 'xtensionpress' ),
    		'id'      => 'special_acts',
    		'desc' => __( 'Check all that apply.', 'xtensionpress' ),
    		'type'    => 'multicheck',
    		//'multiple' => true, // Store values in individual rows
    		'options' => array(
    			'Field1' => __( 'Field1', 'xtensionpress' ),
    			'Field2' => __( 'Field2', 'xtensionpress' ),
    			'Field3' => __( 'Field3', 'xtensionpress' ),
    			'Field4' => __( 'Field4', 'xtensionpress' ),
    		),
    		'inline'  => true, // Toggles display to inline
    	) );

    Then to save selected values to related Events Calendar field I do this

    if ( isset( $sanitized_values['special_acts'] ) ) {
    		update_post_meta( $new_submission_id, '_ecp_custom_8', $sanitized_values['special_acts'] );
    	}

    where _ecp_custom_8 is the Events Calendar checkbox field.

    When the form with the above code is run, the special_acts multicheck field is there and when submitting the form all seems to go fine: no errors, all other fields type correctly save their values to their related Events Calendar fields, but the _ecp_custom_8 field shows all the checkboxes unchecked.

    I’ve tried to uncomment the multiple attribute and still no success. The _ecp_custom_8 Events Calendar field is a checkbox; its name and values match the special_acts one. Am I missing something?

    https://www.ads-software.com/plugins/cmb2/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Can you share what value gets saved to the _ecp_custom_8 field? My hunch is that the multiple fields are coming in possibly as a concatenated string of values for the checkboxes, but when trying to output the saved values back to the string, they’re not being separated out again for the individual fields, thus it’s not finding any matches to make checks for.

    Thread Starter sadeepa17

    (@sadeepa17)

    Hi Michael and thanks for your support. I’m literally bumping my head in the wall about this and it is greatly delaying my schedule on this project, so I really hope you can help me.

    How would I check what valued gets saved to the _ecp_custom_8 field? In database nothing get stored for _ecp_custom_8. On the Events Calendar Pro submitted event I just get the 4 checkboxes for _ecp_custom_8 field unchecked, no matter what I checked on cmb2 form submission. Is there another type of check that I could make to give you more info?

    Plugin Author Justin Sternberg

    (@jtsternberg)

    On the Events Calendar Pro submitted event, check the checkboxes, click save, then look in the database to see how it stores the data. Ultimately you will need to translate the CMB2 submitted data into whatever format it stores to the database natively.

    Thread Starter sadeepa17

    (@sadeepa17)

    Ok Justin, Thanks. I’ve done as you suggested. I have saved the _ecp_custom_8 checking Field1 and Field4 and saved. Then on database it shows:

    MetaKey: _ecp_custom_8
    MetaValue: Field1|Field4

    Thread Starter sadeepa17

    (@sadeepa17)

    Sorry double post

    Thread Starter sadeepa17

    (@sadeepa17)

    Just in case I was misunderstood, I haven’t solved my problem; I’ve just posted how the _ecp_custom_8 field stores the values in db. Any help would be greatly appreciated.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Do your CMB2 fields have values of “Field1” and “Field4” for how it’s set up?

    Looks like it’s getting glued together by the “|” character for the actual saving to the _ecp_custom_8 field.

    The next step here would be to intercept things before the render of the field/checkboxes, grab the _ecp_custom_8 field, and use php’s explode function to turn the saved value back into an array.

    $data = 'Field1|Field4'
    $parts = explode( '|', $data );

    Very pseudocode as I don’t know off the top of my head how Events Calendar fetches the fields, or perhaps how you’re intending to. The $parts variable would be an array with indexed values of ‘Field1’ and ‘Field4’ now. You could then use those to determine if each field should be checked or not.

    It’s a bit of manual work, I realize, but it’s what must be done sometimes.

    Thread Starter sadeepa17

    (@sadeepa17)

    Thank you Michael, that helped.

    I have solved with

    if ( isset( $sanitized_values['special_acts'] ) ) {
                    $ecpCbox = implode("|",$sanitized_values['special_acts']);
    		update_post_meta( $new_submission_id, '_ecp_custom_8', $ecpCbox );
    	}

    And now it works. Thank you for addressing me into the right direction, guys.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Multicheck Selected Values Problem’ is closed to new replies.