I am trying to write a validation function for an edit form, in order to do so I am using '$post_id = $form_data['container_id'];' to get the post_id to then get the values for two fields (wpcf-parcel-totes, wpcf-parcel-bags), but I'm getting an error that 'container_id' has an undefined index. I'm confused as I'm using the same syntax in other non-validation functions, and that code works as expected.
I've commented the code below as best as possible to show what I am attempting to do, please let me know where I am falling short here.
function ylstracking_validate_parcel_edit ($field_data, $form_data) {
list($fields,$errors)=$field_data;
//Check to ensure correct from is being used
if ( 437 == $form_data['id']) {
if ($_POST['wpcf-product-type']==1) { //check to ensure correct product type
$post_id = $form_data['container_id']; //get post id
//get current value of totes/bags for selected parcel (aka values to be restocked)
$parcel_totes = get_post_meta($post_id, 'wpcf-parcel-totes', true);
$parcel_bags = get_post_meta($post_id, 'wpcf-parcel-bags', true);
//get id of parent warehouse post
$parent_warehouse = $_POST['@warehouse-parcel_parent'];
//get value of bags/totes-left fields from parent post
$warehouse_bags = get_post_meta($parent_warehouse,'wpcf-warehouse-dt80-bags', true);
$warehouse_totes = get_post_meta($parent_warehouse,'wpcf-warehouse-dt80-totes', true);
//get id of parent transaction (aka po/purchase order)
$parent_po = $_POST['@transaction-parcel_parent'];
//get value of bags/totes-left fields from parent transaction
$po_bags = get_post_meta($parent_po,'wpcf-trans-dt80-bags', true);
$po_totes = get_post_meta($parent_po,'wpcf-trans-dt80-totes', true);
//double parent warehouse for comparison
$transaction_warehouse = toolset_get_related_post( $parent_po, 'warehouse-transaction' );
//add the current values for totes/bags to what is currently in the warehouse
$warehouse_available_bags = $parcel_bags + $warehouse_bags;
$warehouse_available_totes = $parcel_totes + $warehouse_totes;
//add the current values for totes/bags to what is currently in the original po
$po_available_bags = $parcel_bags + $po_bags;
$po_available_totes = $parcel_totes + $po_totes;
//ensure the correct warehouse was selected on the edit form
if ($parent_warehouse != $transaction_warehouse) {
$errors['wpcf-parcel-totes'] = 'The PO you selected is no located in the Warehouse you selected';
} //ensure bags being shipped out does not exceed what is in the warehouse + restocked values
if ($_POST['wpcf-parcel-bags'] > $warehouse_available_bags) {
$errors['wpcf-parcel-bags'] = 'The warehouse you selected does not have enough DT80 bags';
}
//ensure totes being shipped out does not exceed what is in the warehouse + restocked values
if ($_POST['wpcf-parcel-totes'] > $warehouse_available_totes) {
$errors['wpcf-parcel-totes'] = 'The warehouse you selected does not have enough DT80 totes';
}
//ensure bags being shipped out does not exceed what is in the po + restocked values
if ($_POST['wpcf-parcel-bags'] > $po_available_bags) {
$errors['wpcf-parcel-bags'] = 'The PO you selected does not have enough DT80 bags';
}
//ensure totes being shipped out does not exceed what is in the po + restocked values
if ($_POST['wpcf-parcel-totes'] > $po_available_totes) {
$errors['wpcf-parcel-totes'] = 'The PO you selected does not have enough DT80 totes';
}
}
}
}
Well - I just checked with our next level support and we found that container_id is not supported with "cred_form_validate" hook.
You should try to use the following alternative way - instead of using container_id use the global $post:
global $post;
$post_id = $post->ID; //get post id
//get current value of totes/bags for selected parcel (aka values to be restocked)
$parcel_totes = get_post_meta($post_id, 'wpcf-parcel-totes', true);
$parcel_bags = get_post_meta($post_id, 'wpcf-parcel-bags', true);