Home › Forums › Backend Issues (wp-admin) › Null values are being saved even after error in validate_save_post hoook.
Null values are being saved even after the validation error in the validate_save_post hook.
/**
* Validate the save invoice post.
*/
public function validate_save_post() {
// Check post type is invoice.
if ( 'ims_invoice' !== get_post_type() ) {
return;
}
// Initialze the valid to true.
$is_valid = true;
// Invoice items repater and clone field keys.
$ims_invoice_items_repeater_field_key = 'field_5c4034030cff9';
$ims_invoice_items_repeater_field_clone_field_key = 'field_5c443f4dd87de';
// Field keys for items item.
$ims_invoice_items_item_key = $ims_invoice_items_repeater_field_clone_field_key . '_field_5c443eab22fe1';
$ims_invoice_items_item_quantity_key = $ims_invoice_items_repeater_field_clone_field_key . '_field_5c443eba22fe2';
$ims_invoice_items_item_unit_price_key = $ims_invoice_items_repeater_field_clone_field_key . '_field_5c443ec622fe3';
$ims_invoice_items_item_price_key = $ims_invoice_items_repeater_field_clone_field_key . '_field_5c443edf22fe4';
$ims_invoice_items_item_discount_key = $ims_invoice_items_repeater_field_clone_field_key . '_field_5c443efd22fe5';
$ims_invoice_items_item_amount_key = $ims_invoice_items_repeater_field_clone_field_key . '_field_5c443f0a22fe6';
// Field keys of invoice.
$ims_invoice_sub_total_key = 'field_5c41b2ff17b09';
$ims_invoice_discount_key = 'field_5c41b31a17b0b';
$ims_invoice_vat_key = 'field_5c41b32617b0c';
$ims_invoice_total_key = 'field_5c41b33317b0d';
$ims_invoice_paid_key = 'field_5c41b33b17b0e';
$ims_invoice_due_key = 'field_5c41b34517b0f';
// Get the items list.
$ims_invoice_items = isset( $_POST['acf'][ $ims_invoice_items_repeater_field_key ] ) ? wp_unslash( $_POST['acf'][ $ims_invoice_items_repeater_field_key ] ) : array();
// Initialize sub total.
$calculated_sub_total = 0.0;
// Stores items id, input key and quantitiy.
$items_info = [];
// Loop through each item and validate each item.
foreach ( $ims_invoice_items as $key => $ims_invoice_item ) {
// HTML Input key.
$ims_invoice_items_unit_price_input = 'acf[' . $ims_invoice_items_repeater_field_key . '][' . $key . '][' . $ims_invoice_items_item_unit_price_key . ']';
$ims_invoice_items_price_input = 'acf[' . $ims_invoice_items_repeater_field_key . '][' . $key . '][' . $ims_invoice_items_item_price_key . ']';
$ims_invoice_items_amount_input = 'acf[' . $ims_invoice_items_repeater_field_key . '][' . $key . '][' . $ims_invoice_items_item_amount_key . ']';
$ims_invoice_items_quantity_input = 'acf[' . $ims_invoice_items_repeater_field_key . '][' . $key . '][' . $ims_invoice_items_item_quantity_key . ']';
// Validate unit price.
$ims_invoice_items_item_id = $ims_invoice_items[ $key ][ $ims_invoice_items_item_key ];
$ims_product_mrp = round( floatval( get_field( 'ims_product_mrp', $ims_invoice_items_item_id ) ), 2 );
$ims_invoice_items_unit_price = round( floatval( $ims_invoice_items[ $key ][ $ims_invoice_items_item_unit_price_key ] ), 2 );
if ( $ims_product_mrp !== $ims_invoice_items_unit_price ) {
acf_add_validation_error( $ims_invoice_items_unit_price_input, 'Invalid unit price.' );
$is_valid = false;
}
// Validate price which is calculated in the client side.
$ims_invoice_items_item_quantity = intval( $ims_invoice_items[ $key ][ $ims_invoice_items_item_quantity_key ] );
$ims_invoice_items_item_price = round( floatval( $ims_invoice_items[ $key ][ $ims_invoice_items_item_price_key ] ), 2 );
$calculated_price = round( $ims_invoice_items_item_quantity * $ims_product_mrp, 2 );
if ( $calculated_price !== $ims_invoice_items_item_price ) {
acf_add_validation_error( $ims_invoice_items_price_input, 'Invalid price.' );
$is_valid = false;
}
// Add items id, input key and quantitiy which is fetched in POST.
$items_info[] = array(
'id' => $ims_invoice_items_item_id,
'input' => $ims_invoice_items_quantity_input,
'quantity' => $ims_invoice_items_item_quantity,
);
// Validate amount which is calculated in the client side.
$ims_invoice_items_item_discount = round( floatval( $ims_invoice_items[ $key ][ $ims_invoice_items_item_discount_key ] ), 2 );
$ims_invoice_items_item_amount = round( floatval( $ims_invoice_items[ $key ][ $ims_invoice_items_item_amount_key ] ), 2 );
$calculated_amount = round( $calculated_price - $calculated_price * $ims_invoice_items_item_discount / 100.0, 2 );
if ( $calculated_amount !== $ims_invoice_items_item_amount ) {
acf_add_validation_error( $ims_invoice_items_amount_input, 'Invalid amount.' );
$is_valid = false;
} else {
$calculated_sub_total += $ims_invoice_items_item_amount;
}
}
// echo '<h1>Items Info</h1>';
// var_dump( $items_info );
// Get the items list stored in the database.
$stored_items_info = [];
if ( have_rows( 'ims_invoice_items' ) ) {
while ( have_rows( 'ims_invoice_items' ) ) {
the_row();
$stored_items_info[] = array(
'id' => get_sub_field( 'item_item' )->ID,
'quantity' => get_sub_field( 'item_quantity' ),
);
}
}
// echo '<h1>Stored Items Info</h1>';
// var_dump( $stored_items_info );
// exit;
// Create an array of product id and available stock.
$diff_quantitites = [];
$available_stocks = [];
$index = 0;
foreach ( $items_info as $item_info ) {
// Get the product/item id.
$item_id = $item_info['id'];
// If product quantity is not fetched, fetch it first.
if ( ! isset( $available_stocks[ $item_id ] ) ) {
$available_stocks[ $item_id ] = intval( get_field( 'ims_product_available_stock', $item_id ) );
}
// Get the product/item quanitity passed in the POST.
$item_quantity = $item_info['quantity'];
// Get the product/item quantitiy stored in the database.
$stored_item_quantity = isset( $stored_items_info[ $index ]['quantity'] ) ? $stored_items_info[ $index ]['quantity'] : 0;
// Calculate the differene in quantity stored in database and the quantity send in POST.
$diff_quantity = $item_quantity - $stored_item_quantity;
// Add the diff quantity.
$diff_quantitites[ $item_id ] += $diff_quantity;
// Check if the quantity to be allocated is greater than the avaiable stock.
if ( $diff_quantitites[ $item_id ] > $available_stocks[ $item_id ] ) {
acf_add_validation_error( $item_info['input'], "Not enough stock. Only {$available_stocks[ $item_id ]} available." );
$is_valid = false;
} else {
$available_stocks[ $item_id ] -= $diff_quantity;
}
++$index;
}
// echo '<h1>Difference Quantitites</h1>';
// var_dump( $diff_quantitites );
// echo '<h1>Available Stocks</h1>';
// var_dump( $available_stocks );
// exit;
// Get the sub total and validate sub total.
$ims_invoice_sub_total = isset( $_POST['acf'][ $ims_invoice_sub_total_key ] ) ? sanitize_text_field( wp_unslash( $_POST['acf'][ $ims_invoice_sub_total_key ] ) ) : null;
$ims_invoice_sub_total = round( floatval( $ims_invoice_sub_total ), 2 );
if ( $calculated_sub_total !== $ims_invoice_sub_total ) {
$ims_invoice_items_sub_total_input = 'acf[' . $ims_invoice_sub_total_key . ']';
acf_add_validation_error( $ims_invoice_items_sub_total_input, 'Invalid sub total.' );
$is_valid = false;
}
// Get the invoice discount.
$ims_invoice_discount = isset( $_POST['acf'][ $ims_invoice_discount_key ] ) ? sanitize_text_field( wp_unslash( $_POST['acf'][ $ims_invoice_discount_key ] ) ) : null;
$ims_invoice_discount = round( floatval( $ims_invoice_discount ), 2 );
// Get the invoice vat.
$ims_invoice_vat = isset( $_POST['acf'][ $ims_invoice_vat_key ] ) ? sanitize_text_field( wp_unslash( $_POST['acf'][ $ims_invoice_vat_key ] ) ) : null;
$ims_invoice_vat = round( floatval( $ims_invoice_vat ), 2 );
// Calculate the total and validate the total.
$ims_invoice_total = isset( $_POST['acf'][ $ims_invoice_total_key ] ) ? sanitize_text_field( wp_unslash( $_POST['acf'][ $ims_invoice_total_key ] ) ) : null;
$ims_invoice_total = round( floatval( $ims_invoice_total ), 2 );
$calculated_sub_total_with_discount = $calculated_sub_total - $calculated_sub_total * $ims_invoice_discount / 100.0;
$calculated_sub_total_with_vat = $calculated_sub_total_with_discount + $calculated_sub_total_with_discount * $ims_invoice_vat / 100.0;
$calculated_total = round( $calculated_sub_total_with_vat, 2 );
if ( $calculated_total !== $ims_invoice_total ) {
$ims_invoice_items_total_input = 'acf[' . $ims_invoice_total_key . ']';
acf_add_validation_error( $ims_invoice_items_total_input, 'Invalid total.' );
$is_valid = false;
}
// Get the invoice paid , due and calculate the due as well as validate the due.
$ims_invoice_paid = isset( $_POST['acf'][ $ims_invoice_paid_key ] ) ? sanitize_text_field( wp_unslash( $_POST['acf'][ $ims_invoice_paid_key ] ) ) : null;
$ims_invoice_paid = floatval( $ims_invoice_paid );
$ims_invoice_due = isset( $_POST['acf'][ $ims_invoice_due_key ] ) ? sanitize_text_field( wp_unslash( $_POST['acf'][ $ims_invoice_due_key ] ) ) : null;
$ims_invoice_due = floatval( $ims_invoice_due );
$calculated_due = round( $calculated_total - $ims_invoice_paid, 2 );
if ( $calculated_due !== $ims_invoice_due ) {
$ims_invoice_items_due_input = 'acf[' . $ims_invoice_due_key . ']';
acf_add_validation_error( $ims_invoice_items_due_input, 'Invalid due.' );
$is_valid = false;
}
if ( $is_valid ) {
foreach ( $diff_quantitites as $id => $diff_quantity ) {
$ims_product_available_stock = intval( get_field( 'ims_product_available_stock', $id ) );
$ims_product_available_stock -= $diff_quantity;
update_field( 'ims_product_available_stock', $ims_product_available_stock, $id );
}
}
}
Here is the screenshot.
The topic ‘Null values are being saved even after error in validate_save_post hoook.’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.