Skip Navigation

[Resolved] Validating form with parent post fields

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

Problem:
Validating form with parent post fields

Solution:
You can use Toolset Forms hook "cred_form_validate" in order to add validations.

You can find the proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/validating-form-with-parent-post-fields/page/2/#post-1119194

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

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

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

Author
Posts
#1112415
Capture.JPG

I need to validate the value of a parent posts field(s) prior to submitting a post form. So I'll need to be able to access the value of the drop down ('Select Originating Container' in the screen shot) to then grab the bags and totes field of the parent and compare to the totes and bags values in the form.

For context, I am building an inventory tracking system in which containers (CPT) consist of multiple bags or totes. Those bags/totes then get placed into parcels (CPT) via post form. The container that the bags/totes come from is a parent to the parcel. In the screenshot you will see I'm also using a view to display the current inventory by container for reference.

This is the code I'm using for the validation, I'm sure that at the very least the syntax to get the parent container id isn't correct, but I wasn't able to find an example like this in the docs or the support forum.


function ylstracking_validate_parcel ($error_fields, $form_data) {

	list($fields,$errors)=$error_fields;

	//Check to ensure correct from is being used, 'add outbound parcel form is form 1971 in dev'
	if ($form_data['id']==1971) {

		//get id of parent post
		$parent_container = $_POST['@container-parcel.parent']['value'];
		//get value of bags-left field from parent post
		$available_bags = get_post_meta( $parent_container, 'wpcf-container-bags-left', true);
		//compare the value entered in the form to the value of available_bags
		if ( ($fields['wpcf-container-bags']['value'] > $available_bags) {

			$errors['wpcf-container-bags']='The container you selected does not have enough bags';
		}

			 
	}


}

add_filter('cred_form_validate','ylstracking_validate_parcel',10,2);

I've gone through and utilized info from the following documentation and support threads thus far:

https://toolset.com/documentation/post-relationships/selecting-parent-posts-using-forms-create-child-items/

https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

https://toolset.com/forums/topic/cant-access-select-field-value-in-custom-formvalidator/

Thanks in advance!

#1112707

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - could you please share access details with problem URL and information about what fields you want to validate.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

As you already shared the link:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
We - we share a few common examples there but in your case I can help you out.

#1112981

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - will you be able to set up a staging test site where I can look at the issue?
OR
I need duplicator package of your site with problem URLs to look at the issue.
=> https://toolset.com/faq/provide-supporters-copy-site/

I have set the next reply to private which means only you and I have access to it.

#1114974

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - the duplicator is downloaded, I will get back to you as soon as possible.

#1115049

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

could you please try following code and let me know if it works for you:

function ylstracking_validate_parcel ($field_data, $form_data) {

	/*Check to ensure correct from is being used, 'add outbound parcel form is form 1971 in dev'
	if ($form_data['id']==1971) {


		$parent_container = $_POST['@container-parcel.parent']['value'];

		
	}*/

	 list($fields,$errors)=$field_data;
    if ( 1971 == $form_data['id'] ) {
     
	  //get id of parent post
        $parent_container = $_POST['@container-parcel.parent'];
        //get value of bags-left field from parent post
         $available_bags = get_post_meta($parent_container,'wpcf-container-bags-left', true);
		  
        //compare the value entered in the form to the value of available_bags
        if ($_POST['wpcf-container-bags'] > $available_bags) {
 
            $errors['wpcf-container-bags'] = 'The container you selected does not have enough bags';
      }

    }
    return array($fields,$errors);


}
add_filter('cred_form_validate','ylstracking_validate_parcel',10,2);
#1115142

Hi Minesh,

Thanks for taking a look at this for me. I tested the code you sent over, and it looks like there is still something that isn't working correctly within the function. Regardless of how many bags I input for the parcel, the validation fails and I get the error message outlined in the function. I am also getting two other error messages when submitting a new parcel in reference to the relationships that are set up. I'm a little confused regarding the relationship messages since no relationships are being established due to the parcel not being saved. The error messages are below, let me know if you need further info, I'm continuing to look into the relationship messages now as well.


The post was not saved because of the following 3 problems:
The element CRED Auto Draft 837ec5754f503cfaaee0929fd48974e7 has already the maximum allowed amount of associations (1) as in the relationship Outbound Shipments Parcels.
The element CRED Auto Draft 837ec5754f503cfaaee0929fd48974e7 has already the maximum allowed amount of associations (1) as in the relationship Containers Parcels.
Container Bags: The container you selected does not have enough bags

Thanks,
Matt

#1115482

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The element CRED Auto Draft 837ec5754f503cfaaee0929fd48974e7 has already the maximum allowed amount of associations (1) as in the relationship Outbound Shipments Parcels.
The element CRED Auto Draft 837ec5754f503cfaaee0929fd48974e7 has already the maximum allowed amount of associations (1) as in the relationship Containers Parcels.
==> Well - when creating the relationship - have you set any limit that the number of relationship posts can be created? or there must the dummy relation posts exists. Could you please try to delete all dummy data and check.

Additionally, once I created entry successfully, I do not see any item to select for "Select Outbound Shipment" field. How can I enable so that each time option should be available there.

#1115729

Minesh,

I didn't set any limits when creating the relationships, they are both one to many, so one container can be parent to many parcels, and one outbound shipment can have many parcels. The basic workflow is this:

Inbound shipment has multiple containers -> each container can be split up into multiple parcels -> multiple parcels make up an outbound shipment.

As you requested I deleted out all dummy data and I'm still getting the same error message as before when I walk through the workflow again. I wanted to see if maybe there was something about the one to many relationships that was causing the issue, so I changed them to "many to many" relationships, but when doing so the drop downs show an error on the relationship fields, and when I go into the post form the button to add the relationship fields no longer appears so it looks like I can't test it that way either. This is most likely expected behavior, in a many to many you really don't have a parent/child relationship.

The "Select Outbound Shipment" field on the add parcel page should auto-populate with any published outbound shipment items. They can be created through the "outbound transaction" page, which reminds me I need to change the name of that page to match the "outbound shipment" nomenclature.

I'll continue to investigate on my end, please let me know if you find anything or if you have further questions for me.

Thanks,
Matt

#1116451

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I'll check this tomorrow.

#1117176

Minesh,

Any movement on this? I'm coming up against a milestone deadline and really need to get this figured out. Please let me know if you've found anything as soon as you can.

Thanks,
Matt

#1117180

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Sorry for being a bit late but the forum was really busy. I already installed the duplicator package:

First of all - After checking to debug information you shared with us, I found that you are using outdated Toolset plugins. We always recommend running your site with latest stable release plugin version.

*** Please make a FULL BACKUP of your database and website.***
Could you please update ALL Toolset plugins to it's latest official released version. You can download the latest plugin release from your accounts page:
=> https://toolset.com/account/downloads/

Do you still see issue after updating all Toolset plugin?

#1117202

Thanks for getting back to me Minesh. I've updated all the toolset plugins, and while I'm no longer getting any errors, the validation doesn't seem to be working at all now. I can add a parcel with however many bags or totes I want regardless of how many are in inventory for the selected container. Let me know if you need more info.

#1117212

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - your site is complicated and I still do not have time review whole skeleton. Duplicator you provided has now become useless for me as I cant select any products now with the parent selector.

Would you mind to set up a test site on some testing server where I can see it and if needed I will make a clone from there by installing duplicator plugin.

I have set the next reply to private which means only you and I have access to it.

#1117217

I'll get a dev instance set up and get you access to it. It likely won't be ready until later this afternoon just so you have an idea of time frame. I'll reply back with the details when I have them. Thanks Minesh.

#1117219

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

However, I'm closing here and it's weekend now. So I will only be able to work on Monday now.

I have set the next reply to private which means only you and I have access to it.