Skip Navigation

[Résolu] How to make the repeatable fields group on a form default to the parent CPT

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I have a Form that creates Job posts. That form is set to redirect to another page that contains another Form that creates RFGs for that Job post. I would like to make sure these RFGs are automatically associated with the correct Job post.

Solution: You'll have to use a bit of custom code to add a URL parameter to the first Form's redirect.

add_filter('cred_success_redirect', 'custom_redirect_job_rfg',10,3);
function custom_redirect_job_rfg($url, $post_id, $form_data)
{
  $forms = array( 94 );
  if ( in_array( $form_data['id'], $forms ) ) {
    return $url . '?jobid=' . $post_id;
  }
  return $url;
}

Then in the Form that creates RFGs, set the Job field default value using the 'jobid' URL parameter. Use CSS to hide this field if necessary.

The new RFG Form must not use AJAX if you want to continue to display the Form after submission and keep the Job selected.

This support ticket is created Il y a 4 années et 7 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Auteur
Publications
#1330507

I have a Front-End form to create a CPT (job) that includes repeating fields group (separate form for rooms) on this site :
hidden link

When the user enters the 1st form for a new job then clicks on the "Add Rooms" button to add the repeating fields for an undetermined number of rooms there is a field on that form that has a dropdown for the user to select which "job" this room belongs too. What I would like to have happen is the "job" defaults to the current job that the user just entered. Also can this field be required? I could find a way?

The reason is in testing this I was able to add a room and NOT select the parent"job" the room was added (or so it appears?) but did not get added to the "job". So where is this room now and how to I get it assigned to the job?

Also, please be aware that I will also be creating Front-End "job" editing forms so that the users can comeback and add rooms to existing "jobs" and to edit existing rooms to existing jobs. So what ever the solution is must allow for this as well.
Thank You!

#1330811

Hi, can you tell me how the "Add Rooms" button was added? What is the URL of the destination when someone clicks "Add Rooms"?

Normally you will pass a URL parameter into the page where the new Room Form is displayed. The value of that URL parameter will be the Job post ID. Then in the new Room Form, you can set the value of the parent Job input field using that URL parameter. Let's try to get that set up first, then we can discuss how to make the field required and how to retrieve that Room post that is orphaned.

#1330895
FireShot Capture 224 - Edit Post Form ‹ Mold Test Company — _ - https___wordpress-204758-946314.cl.png

Hi Christian!
thank you!...
Ok, I attached a screenshot of how the add rooms repeatable fields group was done.

Basically, I created two forms one is the "Jobs" form that has fields for the entire job. the "submit" button for that form takes the user to a page that has the repeatable forms group. That Form keeps displaying after it is submitted so the users can enter as many rooms as they need.

If it would you help you to have admin access to help me better... just let me know.
Thank you,
Laura

#1330929

Okay the first thing you have to do is add the Job ID into the redirection URL. You can do that with a little bit of custom code. You can add this to your child theme's functions.php file or create a new snippet in Toolset > Settings > Custom Code:

add_filter('cred_success_redirect', 'custom_redirect_job_rfg',10,3);
function custom_redirect_job_rfg($url, $post_id, $form_data)
{
  $forms = array( 12345 );
  if ( in_array( $form_data['id'], $forms ) ) {
    return $url . '?job=' . $post_id;
  }
  return $url;
}

Change 12345 to match your new Job Form ID.

Now when the new Job Form is submitted, you should be redirected to a URL that contains the new Job ID as a URL parameter. Let me know if you are able to get that working and we can go from there.

#1331555

Hi christian -
Ok I added the code and tested by trying to add a new job. When I clicked on the (submit "Add Room") button I got a 404 page not found.

The url is:
hidden link

the form # for the new job form is 4457

a little background on my setup... I have two pages to handle these. a page for the "Create New Job" that has the form shortcode of that form. and another page that handles the add new rooms with the shortcode for that form.

what would you recommend I try to resolve this?

#1331637

Okay it seems that the "job" URL parameter is causing a problem here. Let's change that to "jobid" for now by replacing line #6 in the code above with this:

return $url . '?jobid=' . $post_id;

Then in your Form that creates RFG, find the parent Job input field. You can set this field to "set default value using a URL parameter", jobid. Now when the Form loads, the correct Job should be selected automatically. Once that is working as expected, we can add some CSS to hide the Job field if you'd like. It depends on whether or not you expect Users to visit this Form directly, without submitting the Job form first. In those cases, you would want to show the Job input field.

#1331659

Ok, that worked... it successfully redirected to the page with the RFG and defaulted to the parent job.

I think we would want to hide this field on the Add New Jobs/rooms BUT when we get to the editing forms I believe we may want to see it... I think? It depends on how we have to set up the editing forms maybe...

I would like to have the users be able to edit the main parent job fields and to ALSO be able to edit each of the rooms fields as well.

Also I would like to have them to be able to delete rooms and possibly jobs as well. But that would not be a show stopper as I can always go to the back-end and delete them for them if necessary.

#1331663

I think we would want to hide this field on the Add New Jobs/rooms BUT when we get to the editing forms I believe we may want to see it... I think?
Okay to hide the input in the new Room Form, you'll have to turn on "expert mode" in the Form builder. Then you can wrap the parent input field in a div tag that includes some CSS to hide the field:

<div style="display:none;">
  ...include your parent Job field shortcode here...
</div>

I would like to have the users be able to edit the main parent job fields and to ALSO be able to edit each of the rooms fields as well.
Sure, you can use edit post Forms to handle these individually. The current software only allows you to edit one post (or one RFG) in a single Form, so keep that in mind.

Also I would like to have them to be able to delete rooms and possibly jobs as well. But that would not be a show stopper as I can always go to the back-end and delete them for them if necessary.
In a View of Rooms, you can include a "Delete post" link in the loop to delete that Room. Similarly in a Job post template, you can include a "delete post" link to delete the Job. You can insert that link from the Toolset Forms button popup above the text editor area.

#1331675

Thank you, but what is the "parent Job field shortcode" ? where would i find that?

#1331691
Screen Shot 2019-09-04 at 11.56.39 AM.png

When you turn on "expert mode" in the Form builder, you can find the shortcode that corresponds to the Jobs field in the form editor area. It will look something like this:

[cred_field field='@relationship-slug.parent' class='form-control' output='bootstrap' select_text='--- not set ---']

If you're not sure, you can copy + paste the entire Form editor code here for me to review and I'll find it.

#1331701

Nevermind - I understand.... made sense once I switched to expert mode..

Ok, so now I was able to create an Edit form for the main Job form and then added a "link" to "Edit" it on the view/content template that when clicked displayed the editing form. But I am having trouble trying to do that for each of the rooms... do i need to create another "page" and place that Rooms group editing form on it? right now I have added an Edit link to the view/content template that shows the rooms but when i click on it nothing happens.

Let me know if that made sense? Thanks!

#1331709

well... I discovered a problem... now it will not save more than 1 room when adding a new job?

#1331725

I unhide the job field and when I submit the 1st room. the form displays again to be able to enter the next room but the "job" field is not set anymore?

#1331761
Screen Shot 2019-09-04 at 1.15.45 PM.png

But I am having trouble trying to do that for each of the rooms... do i need to create another "page" and place that Rooms group editing form on it? right now I have added an Edit link to the view/content template that shows the rooms but when i click on it nothing happens.
To edit a single row of the RFG, you will create an "edit post form" for this RFG and insert it in an unassigned Content Template. Then when you insert an "Edit Post" link in the View, you will be asked to select the Content Template that includes your Edit Post Form. When you click the Edit Post link, it should redirect you to a page that contains the edit post form. If that's not working as expected, make sure you're using the correct "Edit Post Link", as shown in this screenshot. It's in the popup when you click "Toolset Forms".

It might be best if I can see how everything is set up in wp-admin. Can I get a login? Private reply fields are enabled here. Let me know how where I can submit a Job so I can start the process and follow along.

#1331775

Hi don't worry about the "editing" forms just yet... We have a problem with the jobid not staying set when entering multiple rooms for a new job.
For now, I have disabled AJAX submissions for this Form. That seems to solve the problem, at least temporarily. I will do some additional investigation to see if this is a known issue, or if there is a better workaround.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.