Skip to content

Commit

Permalink
Fix: display errors on donation form when checkout validation fails f…
Browse files Browse the repository at this point in the history
…or v2 forms (#6996)
  • Loading branch information
ravinderk authored Oct 13, 2023
1 parent 8b22394 commit 7b9e9c4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
4 changes: 2 additions & 2 deletions assets/src/js/frontend/give-ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ jQuery( document ).ready( function( $ ) {
$.post( Give.fn.getGlobalVar( 'ajaxurl' ), this_form.serialize() + '&action=give_process_donation&give_ajax=true', function( data ) {
if ( $.trim( data ) == 'success' ) {
//Remove any errors
this_form.find( '.give_errors' ).remove();
this_form.parent().find( '.give_errors' ).remove();
//Submit form for normal processing
$( give_purchase_form ).submit();

Expand All @@ -263,7 +263,7 @@ jQuery( document ).ready( function( $ ) {
} else if ($giveFormHeader.length > 0) { // Classic Form
$giveFormHeader.after(data);
} else { // Legacy Form
this_form.parent().find('.give-form-title').after(data);
this_form.parent().prepend(data);
}

this_form.parent()[0].scrollIntoView({behavior: 'smooth'});
Expand Down
52 changes: 24 additions & 28 deletions includes/class-notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,36 +282,32 @@ public function render_admin_notices() {
}


/**
* Render give frontend notices.
*
/**
* Render give frontend notices.
*
* @unreleased Render errors on Ajax request (Donation form validation - v2 forms)
* @since 2.32.0 Display registered error on donation form.
* @since 1.8.9
* @access public
*
* @param int $form_id
*/
public function render_frontend_notices( $form_id = 0 ) {
$errors = give_get_errors();

$request_form_id = isset( $_REQUEST['form-id'] ) ? absint( $_REQUEST['form-id'] ) : 0;

// Sanity checks first:
* @since 1.8.9
* @access public
*
* @param int $form_id
*/
public function render_frontend_notices($form_id = 0)
{
$errors = give_get_errors();

$request_form_id = isset($_REQUEST['form-id']) ? absint($_REQUEST['form-id']) : 0;

// Sanity checks first:
// - Ensure that gateway returned errors display on the appropriate form.
// - Error should not display on AJAX request.
if (
isset( $_POST['give_ajax'] )
|| ( $request_form_id && $request_form_id !== $form_id )
) {
return;
}

if ( $errors ) {
self::print_frontend_errors( $errors );

give_clear_errors();
}
}
// - Error should exist.
if (! $errors || ($request_form_id && $request_form_id !== $form_id)) {
return;
}

self::print_frontend_errors($errors);
give_clear_errors();
}

/**
* Renders notices for different actions depending on
Expand Down

0 comments on commit 7b9e9c4

Please sign in to comment.