Skip Navigation

[Resolved] cred_generic_field values refresh on error, values dont persist

This thread is resolved. Here is a description of the problem and solution.

Problem:

I have a CRED form with a few cred_generic_field s, when I submit the form and I get a validation error (no email supplied etc) the values for the multi-select form are lost.

Solution:

There isn't such a built-in feature within Toolset Forms plugin, it needs custom codes, for example, you can try these:

https://toolset.com/forums/topic/cred_generic_field-values-refresh-on-error-values-dont-persist/#post-1422809

Relevant Documentation:

100% of people find this useful.

This support ticket is created 4 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 2 voices.

Last updated by stuart 4 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#1422423

I have a CRED form with a few cred_generic_field s, when I submit the form and I get a validation error (no email supplied etc) the values for the multi-select form are lost.

[cred_generic_field type='multiselect' field='generic-industry-taxonomy' class='ind-taxonomy']
{
"required":1,
"persist":1,
"default":[],
"options":[[wpv-view name="partner-industries-select"]]
}
[/cred_generic_field]

here is a short video with what I mean... hidden link

I have tried a number of other localstorage jquery code, but to no avail.

could someone give some advice on why this may be.

thanks

#1422809

Hello,

There isn't such a built-in feature within Toolset Forms plugin, it needs custom codes, for example, you can try these:
1) Create a custom shortcode to get the PHP post var "generic-industry-taxonomy" value, add below codes into your theme file functions.php:

add_shortcode('get_post_var', function($atts, $content){
	$atts = shortcode_atts( array(
		'slug' => 'generic-industry-taxonomy',
	), $atts);
	$res = '';
	if(isset($_POST[$atts['slug']])){
		$res = implode(',', $_POST[$atts['slug']]);
	}
	return $res;
});

2) Dashboard-> Toolset-> Settings-> Front-end Content, in section "Third-party shortcode arguments", add the shortcoe name: get_post_var

3) Edit your post form, find and replace the generic field shortcode, replace this line from:
"default":[],

With:
"default":[[get_post_var]],

And test again.

#1428803

hi Luo, thanks for the assistance... its not quite behaving, here is where we are at:

Ive modified your code as it didn't render the short-code data for some reason and a slightly modified version didn't include the inverted commas around the values, but this does (below):

However, something is not quite right.

    function get_post_var_saved($atts, $content){
    $atts = shortcode_atts( array(
        'slug' => 'generic-industry-taxonomy',
    ), $atts);
    //$res = '';
    if(isset($_POST[$atts['slug']])){
      	$res = implode('","', $_POST[$atts['slug']]);
    }
    return '"'.$res.'"';
}

add_shortcode('get_post_var', 'get_post_var_saved');

This is the short-code: [get_post_var]

It outputs fine and in the correct format from what I can see:

---> direct output: "wireless","wine-spirits"

However, when I use the short-code and click submit if there is more than one selected the field disappears - see image.

One option selected: hidden link

Multiple selected: hidden link

[cred_generic_field type='multiselect' field='generic-industry-taxonomy' class='ind-taxonomy']
{
"required":1,
"persist":1,          
"default":[[get_post_var]],
"options":[[wpv-view name="partner-industries-select"]]
}

Image from CRED editor: hidden link

If I used the short-code output directly in the default field (just to check on the output formatting, then check on the front end it renders the values as expected.
See image: hidden link

So must there be something else in the short-code output that is breaking it? I can see where... and I have been playing around for ages trying to figure it out..

The default values filled with the shortcode output below:

[cred_generic_field type='multiselect' field='generic-industry-taxonomy' class='ind-taxonomy']
{
"required":1,
"persist":1,          
"default":["wireless","wine-spirits"],
"options":[[wpv-view name="partner-industries-select"]]
}

thanks for the help

#1429621

My issue is resolved now. Thank you!

Ok, so thought it was weird this wasn't working... clean short-code output and everything about it looked good...
views short-codes worked... just not this one.

I woke up at 4 am and had an idea, what about registering this as a "Third-party shortcode arguments" in settings >Front-end content....

It worked. hours of playing around but it worked - I now have a solution:)

Now all I need to adjust the code to allow for short-code arguments so the code can be reused easily on multiple taxonomies - which I believe I just did... if anyone else needs it (** I hope its correct, worked on this one **)

short code: [get_post_var slug="generic-industry-taxonomy"]

(replace the slug with your own taxonomy slug obviously)

    function get_post_var_saved($atts, $content){
    extract(shortcode_atts( array(
        'slug' => -1,
    ), $atts));
	
	if($slug == -1) return '';
	
    $res = '';
    
	if(isset($_POST[$slug])){
      	$res = implode('","', $_POST[$slug]);
    }
    return '"'.$res.'"';
}

add_shortcode('get_post_var', 'get_post_var_saved');
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.