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 5 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.

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
#1119067

Minesh
Supporter

Languages: English (English )

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

Well - I checked the form and I see that the field "wpcf-container-bags" is not available with the form. That is why its not displaying the error.

Is that field is missing from the form?
=> hidden link

#1119126
Add_Parcel.JPG

Minesh,

To answer your question regarding the add-parcel form, no wpcf-container-bags is not missing from the form, and has been replaced in the validation function with the correct field which is wpcf-container-bags-left which is also not on the form. That field is stored under in the parent container and keeps track of the bags left in the container and needs to be compared to the bags being input in the form for validation.

I've tested again after making the changes as described above, and the form is still not validating. I've attached a screenshot of what is being input on the form, you can see that the parent container selected is CON001 which has 10 bags left. I attempt to add 15 bags to the parcel, and it goes through without error. The major issue from what I can tell is that I'm unable to reference the parent container because the below line of code isn't grabbing the parent id.

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

If that line of code is not the issue, please let me know and let's move on to whatever else it could potentially be. This ticket has been open for 12 days and I'm not any closer to finding a solution. This obviously presents a very large issue for both myself and my client as I am unable to continue development on their project. I need to get this closed out in the next 24 hours. If that is not going to be possible due to your current utilization please pull in another individual from the support team to assist so we can get this hammered out.

Thanks,
Matt

#1119194

Minesh
Supporter

Languages: English (English )

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

Could you please check now - I've adjusted the validation code as given under to your current theme's functions.php file:

function ylstracking_validate_parcel ($field_data, $form_data) {

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

	//Check to ensure correct from is being used, 'add outbound parcel form is form 41 in dev'
	if ( 41 == $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-parcel-bags'] > $available_bags) {
  
            $errors['wpcf-parcel-bags'] = 'The container you selected does not have enough bags';
      }
 
    }
    return array($fields,$errors);


}

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

The issue was the field names were not unique in your previous site and this site. I tested with the same parent CON001 and input 15 bags and I can see its throws errors successfully now. Could you please confirm.

#1119206

Minesh, confirmed the validation appears to be working just fine now.

Thank you again for all your help sir, it is sincerely appreciated.

Matt

#1119224

Minesh,

Sorry to reopen, but I wanted to see what you meant by "The field names are not unique". I tried to duplicate the code to validate the container-totes-left against entered parcel-totes in the same manner, but I'm running into the same issue. Is there a duplicate field in the DB? I can't find one via the admin pages.

Thanks,
Matt

#1119266

I figured it out Minesh, it would help if I actually uploaded the functions file into the theme folder I am using ...

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