Skip Navigation

[Resolved] Radio field in create form > to hidden field in edit form

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

Problem:

I'm wanting to do is change the radio field to hidden field on the edit form.

Solution:

You can use [wpv-conditional] shortcode to display different content on custom field value, for example:

https://toolset.com/forums/topic/radio-field-in-create-form-to-hidden-field-in-edit-form/#post-2348465

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-conditional

This support ticket is created 2 years, 7 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.

Our next available supporter will start replying to tickets in about 6.32 hours from now. Thank you for your understanding.

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 10 replies, has 3 voices.

Last updated by TomW5440 2 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#2346065

Tell us what you are trying to do?

So I have a create form which has a radio field. Once this create form is submitted the user can edit the form later via a link. The radio field is a custom field added to my CPT.

What I'm wanting to do is change the radio field to hidden field on the edit form. I can add the hidden field easily enough but I need to somehow set the default value to be the same value set from the radio field previously.

If you could please show me how to do this I'll be able to do it next time.

Thank you!

#2346121

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The simplest way to achieve this is to continue with the radio field in the edit form, but hide it with CSS.

(A hidden input is no more secure inasmuch as it can also be revealed and modified with the browser dev tools.)

You can provide the existing value as a default value for your generic hidden input (which will need the same post meta key; Types custom fields are stored in wp_postmeta with a 'wpcf-' prefix, e.g. a field with slug of "priority" would be stored as "wpcf-priority") using a shortcode to provide the existing field value (e.g. using the wpv-post-field shortcode: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-field).

The problem with this approach is that generic field values are discarded after form submission completes. If you want to save the value you can either use the Forms API (e.g. the cred_save_data filter: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data) to manually save the field value after the form has submitted.

Or you can switch to the Advanced Editor for the form. The generic field includes JSON options, and you can add the option "persist":1 to the existing options which will tell Forms to save the field value.

However, all of the above seems redundant.

If you are hiding (one way or another) the field because you do not want users to be able to change its value, the you can simply omit the field from the form entirely: the original value will be unchanged.

#2346137

If you are hiding (one way or another) the field because you do not want users to be able to change its value, the you can simply omit the field from the form entirely: the original value will be unchanged.

The only reason why I need it is because the edit form has a conditional that displays other fields based on what the value of the radio is. Is there a way I can achieve this without having the radio field in the edit form?

#2346141

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The conditional display in forms is evaluated on the front end using JavaScript, but it sounds like you could use the wpv-conditional shortcodes (which are evaluated on the server when generating the page) to show or not certain form fields.

You would need to use the Advanced Editor for the form to add the opening and closing wpv-conditional shortcode (to wrap the fields you want to selectively display), and you'll need to add them manually.

See https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-conditional for the required format.

#2346147

Thanks Nigel!

So something like -

[wpv–conditional if="( $(page-type) eq '1' )"]  <div class="row">
		<div class="form-group col-md-12">
			<label for="%%FORM_ID%%_my-message-to-the-world">[cred_i18n name='my-message-to-the-world-label']My message to the world[/cred_i18n]</label>
			[cred_field field='my-message-to-the-world' force_type='field' class='form-control' output='bootstrap']
		</div>[/wpv–conditional] 

Where page-type is the custom radio field slug.

Sheesh.. This just displays the shortcode on the front end.

#2346737

Hello,

I assume the custom radio field "page-type" is created with Toolset Types plugin, Types plugin will add prefix "wpcf-" before the field slug, so you can use "wpcf-page-type" in the conditional shortcode, like this:

[wpv–conditional if="( $(wpcf-page-type) eq '1' )"]
...
[/wpv–conditional]

For the issue:
This just displays the shortcode on the front end.
What shortcode display on frontend? please take a screenshot for it, thanks

#2346743
tool1.png
tool2.png

tool2 shows shortcode being used in advanced form editor
tool1 shows the shortcode displaying on the front end

#2346763

In the editing post form, you can follow our document to display/hide content on custom field values:
https://toolset.com/course-lesson/conditional-display-for-form-inputs/

If you are using the "Expert mode", you can setup the conditional codes by clicking button "Add conditional groups".

So you can try these:
1) Use CSS codes to hide the custom radio field "page-type"
2) Use "conditional groups" to hide/show other custom fields.

#2346767

Oh okay so the link I was sent by support - https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-conditional actually doesn't apply to forms?

I have tried using the conditional groups but this only seems to work if the radio field is included in the edit form too. The whole idea is that the radio is hidden or not in the edit form.

Using css to hide the radio wont work as someone can simply inspect console, make it visible again and change value.

#2348465

Yes, [wpv-conditional] shortcode should be able to work withing Toolset post form too, I have tried below codes in my localhost:

[wpv-conditional if="( $(wpcf-page-type) eq '1' )"]
... wpcf-page-type  eq '1' ...
[/wpv-conditional]

It works fine, please try to copy/paste above codes into your post form, and test again

#2348643

My issue is resolved now. Thank you!

It was the spacing before the closing tag

   [/wpv-conditional]