Skip Navigation

[Resolved] Conditionally display the "add new" for repeating fields in post forms

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 0.64 hours from now. Thank you for your understanding.

This topic contains 10 replies, has 2 voices.

Last updated by Don 1 month, 1 week ago.

Assigned support staff: Christian Cox.

Author
Posts
#2085001

Don

Tell us what you are trying to do

i am wanting to provide my users two options for the selected custom fields in a form when creating a post. In one variation the custom fields will be single fields, in another they can have multiple values. But they MUST be the same custom fields so can be filtered in a view via the same search filter. I had planned to just do this using two separate forms using identical same custom fields, but overlooked the fact that whether a custom field can have a single or multiple value is already set in the "custom fields" section for the post type and not in the form itself. So, I am hoping that it is possible to create a conditional that will ONLY show the option to "add new" for a field if 1) the fields are in a specific form (id#) where i want these custom fields to allow multiple values OR 2) there is only a single form and a "extra" custom field ( radio buttons with "single" and "multiple" options) allows or prevents the display of the "add new" option on the fields that are setup to allow multiple values.... if that makes sense. Then i could just set all the fields i want to "allow multiple instances of this field" and enable/disable the display of the actual option on the front end form based on this "extra" custom field value OR based on the form id #...

IF i can get that piece sorted, then i will be extremely relieved, and be able to finalize my post type custom fields, knowing i can provide users the functionality in forms while creating a post as well as filtering a view.... and that will be great.

#2086641

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

I am hoping that it is possible to create a conditional that will ONLY show the option to "add new" for a field if 1) the fields are in a specific form (id#) where i want these custom fields to allow multiple values OR 2) there is only a single form and a "extra" custom field ( radio buttons with "single" and "multiple" options) allows or prevents the display of the "add new" option on the fields that are setup to allow multiple values.... if that makes sense
I think the best way to accomplish this is with multiple Forms, because there is no easy way to create a conditional based on Form ID or based on some extra custom field that is set in wp-admin somewhere - there is no way to add such a field in Toolset Forms. Forms Conditional Groups work best based on the custom field selections on the front-end, not the Form ID. So instead, you could set up multiple Forms. In one Form, the standard fields would be displayed with the ability to add more values. In another Form, you could replace the standard fields with generic fields of the same field type. Those would be displayed as single fields without the ability to 'add more' values. If you use the same slug with the wpcf- prefix in the generic field as the standard field, these fields can save values to the standard custom fields easily to preserve your searching and filtering capability.
So if your standard field slug is "address", your generic field slug should be "wpcf-address". Let me know if you have questions about implementing generic fields in a Form.

#2086739

Don

hi and thanks for the prompt reply, even on a weekend!

wow.... i had no idea that the "general fields" could be used for something like that.... reading up on them i am a bit more confused what the point of including them as options for a form is, given that they dont save anything to the database with the post/form (by default at least).... but i guess if they are there for things like i want to achieve here then thats a great thing that they ARE included haha.

Just to clarify, if i make a "generic" field which is basically a CLONE of a standard custom field (title, type, preselect values etc) and opt to just save the slug with wpcf-(same title as standard custom field) it wont give an error that "that slug is already in use"? because i have seen that error already while attempting other things.... if that is a "workaround trick" to mimick standard custom fields and actually have the value entered in a generic field saved EXACTLY as if it had been entered in a standard custom field of the same slug name, then that is awesome.

i had otherwise hoped that some javascript to make "display: none" on the button with a plus that triggers a new input field for a field that allows multiple select would do the trick, ie something that targeted: class="js-wpt-repadd wpt-repadd dashicons-before dashicons-plus-alt" since, if that button ONLY shows if another field has (for example) "multiple" selected, then by default there would only be one entry possible unless the trigger field had a value set that through javascript changed the display: none to display:inline (or whatever)....

I guess from whichever options would "work" at all, it comes down to the most simple, user friendly option.....

#2088573

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Just to clarify, if i make a "generic" field which is basically a CLONE of a standard custom field (title, type, preselect values etc) and opt to just save the slug with wpcf-(same title as standard custom field) it wont give an error that "that slug is already in use"?
No, you won't get an error like this. Generic field slugs are not validated the same way other custom field slugs are validated in the system, and it's acceptable to use a generic field slug identical to a custom field slug that already exists in the system. You should not use the generic and standard fields with identical slugs in the same Form, to prevent ambiguous reference issues.

There is an additional step to force a generic field to store its contents in a standard custom field with the same slug, and you have two options for how you want to proceed.

Option 1 is you can turn on Expert Mode in the Form builder and set the persist attribute in each generic field's data object like so:

[cred_generic_field type='numeric' field='wpcf-number-1']
{
"default":"123",
"persist":"1"
}
[/cred_generic_field]

Option 2 is you can use a custom code snippet with our cred_save_data PHP API to capture the generic field value and save it using update_post_meta, like so:

add_action('cred_save_data', 'tssupp_generic_field_action',10,2);
function tssupp_generic_field_action($post_id, $form_data) {
  $forms = array( 12345 );
  $number_slug = 'wpcf-number-1';

  if ( in_array( $form_data['id'], $forms ) )
  {
    if( isset($_POST[$number_slug])){
      update_post_meta( $post_id, $number_slug, $_POST[$number_slug]);
    }
  }
}

Option 1 requires no PHP, but it prevents using the drag-and-drop Form builder since the persist attribute for each generic field must be defined in expert mode and will be lost when switching between expert and non-expert mode.

#2088599

Don

thank you for the clarification and code suggestions. Im concerned i am creating too many form variations for a single post type (ie, free vs featured, then form with all forms single value fields and another with identical fields but with some allowing multiple values)..... I am stubborn and hoping i can make the single/multi variation work with the same fields, in one form, without any complicated workaround or pseudo fields. Although knowing that could be a workaround is indeed awesome, so thanks again.

I am experimenting.... and I created a generic field (but i am thinking it might have to also be a custom field as well if generic forms dont save, since otherwise there wont be any reference carrying over for a post edit i assume) with radio buttons, one value is blank (but could also have a value like "single" if helps to do this better than no value), the other is "floorplan" (which would indicate multiple values possible)..... then I edited the css in the post form to make the "add new" not display with:

a.wpt-repadd {
display: none;
}

and that works. So, IF I can come up with the right javascript conditional, so that IF the radio button value "floorplan" is selected, the a.wpt.repadd is changed to display inline-block and, if not (or value is "single") the style stays as display: none.

Then, if that works, i could basically just use the fields as needed in one form, and the user on front end would only see the option to add more than one value IF that radio button value was selected..... might be a better workaround, or am i seeing it to simply?

#2088679

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Then, if that works, i could basically just use the fields as needed in one form, and the user on front end would only see the option to add more than one value IF that radio button value was selected..... might be a better workaround, or am i seeing it to simply?
I'm not really following how and where you expect to set the dynamic value of this "master" radio button that determines whether the other fields show one or more input options. Can you explain where you imagine this field existing in wp-admin? It's not clear to me. Obviously this is not a visible input in the Form, that would not make sense. First of all, people could just change it and submit additional values. Second of all, the field value could not be dynamic in that case, it would always be the same for all implementations of the Form. Is the master setting arbitrary based on JavaScript you will write and inject into the page somehow when the Form loads? Is it based on the value of a custom field you plan to set in a different post type, where the Form will be displayed in that different post type's template?

#2088761

Don

hi, and thanks for the good question, sorry if i was vague about my intentions or plan.

1) I'm not really following how and where you expect to set the dynamic value of this "master" radio button that determines whether the other fields show one or more input options.
I planned on it being a custom field value in the form, NOT an admin setting. It really isnt a "useful" form field, as in it wont be used for anything else (filtering or display in any templates) but was hoping that using it like that within the form, ie a radio button with two values, blank (or "single) and "floorplan", with javascript would enable the show or hide of that a.wpt-repadd, given that i had changed the default css to display:none.

2) Obviously this is not a visible input in the Form, that would not make sense. First of all, people could just change it and submit additional values.
That is actually exactly what i want.... to be able to give users on the front end an easy OPTION to do the post about a "single" apartment OR as a "floorplan", with the ability to add multiple values for specific fields.

3) Second of all, the field value could not be dynamic in that case, it would always be the same for all implementations of the Form.
Not sure what that means. But, does sound ominous.... but if it means the fields set as allowing "multiple values" would ALWAYS allow multiple values, that is clear to me, and fine..... assuming that my show/hide trick to prevent the display of the "add new" button works, which would effectively make the fields "singular", and in effect only save one value, UNLESS the radio with value "floorplan" was selected, triggering the javascript change to display:inline-block and, thereby, the ability to input multiple values through individual input fields....

but, at the end of the day, allowing the display of the "add new" button or not via javascript, all within one form, isnt necessary either i reckon...your idea to do two separate forms could work, again using the radio button to decide which form to display, with both forms using identical custom fields, including the ones that allow multiple values, and in one form for "single" i can edit the css as mentioned above to prevent the display of the "add new" button functionality, and in the "floorplan" form without the custom css it works like normal.... i got to read up on how that works EDITING a post, when there are multiple forms used for the same post type but that option could work too, just requires two forms...

#2088857

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

That is actually exactly what i want.... to be able to give users on the front end an easy OPTION to do the post about a "single" apartment OR as a "floorplan", with the ability to add multiple values for specific fields.
Oh I see, I was confused about what you wanted. Sounds like a good plan in this case.

#2089591

Don

ugh... back to the drawing board. Totally overlooked the fact that, regardless of whether the user wants to add multiple values for these specific fields or not, the "Images" field should ALWAYS allow the adding of multiple values/images.... so my "duct tape" solution of not displaying the "add new" button via css and javascript wont work since the images field will always use that same add new button and there is no way to specifically hide this button for the other fields without inadvertently hiding it for the image field too. Ugh.

So, the other option i am looking at now is:
1) using a separate set of custom fields that allow "only one value" to "mimic" the standard custom fields which allow multiple, within a single form and
2) set field conditionals for each field, so that the single value ones only display if the radio button with value "single" is selected, and the multiple value ones only display if the "multiple" value is selected. That works.... except that the field labels always display, even if the conditional for the field is not met lol.... kind of odd, one would think that a conditional for a field would also apply to the fields label but apparently not so have to sort that out still.
3) I will figure out a way to assign the "single value" custom field value to the first "multi value" custom field IF the custom field with the single/multi has the value "single" selected.... hopefully, that will basically assign this "duplicate" value to the multi value field.
4) on the front end, for filtering views and displaying fields i will ONLY use the "multi value" fields, the single value field, if used, is only for post creation and edit useful.

Big question: I am not sure how that works with editing a post, if multiple forms are available for a single post type. In another ticket we discussed the options available for featured posts, and the suggestion was, and i followed, to use separate but basically identical forms, one for "free" posts and another for paid/featured posts. But, if someone edits a post, how will the "edit post" link be able to correspond to the correct "edit post" form that corresponds to the matching free vs paid post creation form? And, if i were to change course from my workaround above, and follow the suggestion to yet again use different forms, one for posts with only single value fields and another with a form that allows multi values, but instead of using the generic fields with a function to assign values i did like described above in the "single value" form to assign the single values to (hidden) "multi value" fields, how would that now work with editing a post using the correct edit form that corresponds to the add post form used, ie then i would effectively, for ONE post type, have these forms: 1) Free with single values 2) Free with multi values 3) PAID with single values and 4) PAID with multi values..... so editing any given post is going to have to be done with one of four possible edit post forms.....

#2089631

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

But, if someone edits a post, how will the "edit post" link be able to correspond to the correct "edit post" form that corresponds to the matching free vs paid post creation form?
Each Edit Post Form should be placed in a separate blank Content Template. When you generate an Edit Post Link for a post, you will select the template you want to direct the User to. So you would have one Edit Post Link directing to each Form. You would need to then set up conditionals to display the correct edit post link to the correct Form/Template based on whatever logic you decide to implement, I suspect that's based on the value of one or two specific fields in that post that determine free vs. paid option, and single vs. multiple inputs.

#2089715

Don

My issue isn't resolved yet but you sure gave great advice and pointed me in the right direction. Thank you!