Description

The wpforms_process_validate_payment-single action fires for each Single Item payment field while WPForms validates a submitted form, before any field value is formatted and before the entry is saved.

parametertypedescription
$field_idstring|intField ID as a numeric string.
$field_submitmixedSubmitted field value (raw data). For this field it is the amount string from the request, or an empty string when nothing was posted for the field.
$form_dataarrayForm data and settings.

More Information

WPForms_Process::process() loops over every field in $form_data['fields'] and calls do_action( "wpforms_process_validate_{$field_type}", $field_id, $field_submit, $this->form_data ) for each one. This hook is the instance for the payment-single field type. It fires on every submission of a form that contains the field, whether or not the visitor filled it in. The file lives in includes/, so it fires in Lite and Pro.

This hook is an instance of do_action( "wpforms_process_validate_{$field_type}", $field_id, $field_submit, $form_data ). Other instances are wpforms_process_validate_payment-checkbox for the Checkbox Items field and wpforms_process_validate_email for the Email field.

The field class registers its own validate() on this hook at priority 10 with three arguments. It adds the required-field error. Then, unless the wpforms_forms_fields_payment_single_field_validate_amount filter returns false, it checks the amount: a fixed-price item must post an amount equal to the configured price, and a User Defined item cannot post a negative amount or an amount below the field’s Minimum Price setting.

$field_submit is the raw posted string. Run it through wpforms_sanitize_amount() before comparing it as a number. To reject the submission, write a message to wpforms()->obj( 'process' )->errors[ $form_data['id'] ][ $field_id ]. The wpforms_process_initial_errors filter runs on the collected errors after the loop; Conditional Logic uses it to drop errors from hidden fields.

Source

wpforms/includes/class-process.php

Example

Enforce a minimum amount on a User Defined Single Item field that carries the CSS class set-minimum, and stop the submission with an error under the field when the amount is lower.

/**
 * Enforce a minimum amount on a User Defined Single Item field.
 *
 * @link https://wpforms.com/developers/wpforms_process_validate_payment-single/
 *
 * @param string|int $field_id     Field ID.
 * @param mixed      $field_submit Submitted field value (raw data).
 * @param array      $form_data    Form data and settings.
 */
function wpf_dev_process_validate_payment_single( $field_id, $field_submit, $form_data ) {

    // Only run on my form with ID = 143, remember to change this to match your form ID.
    if ( absint( $form_data['id'] ) !== 143 ) {
        return;
    }

    // Only run on fields that have the CSS class "set-minimum" in the field's Advanced tab.
    $classes = explode( ' ', $form_data['fields'][ $field_id ]['css'] ?? '' );

    if ( ! in_array( 'set-minimum', $classes, true ) ) {
        return;
    }

    $minimum_amount = 5;

    if ( (float) wpforms_sanitize_amount( $field_submit ) < $minimum_amount ) {
        wpforms()->obj( 'process' )->errors[ $form_data['id'] ][ $field_id ] = sprintf(
            /* translators: %s - minimum amount. */
            esc_html__( 'Minimum amount is %s.', 'wpforms' ),
            wpforms_format_amount( $minimum_amount, true )
        );
    }
}
add_action( 'wpforms_process_validate_payment-single', 'wpf_dev_process_validate_payment_single', 10, 3 );

Reference Article