Skip to content

Instantly share code, notes, and snippets.

@kjohnson
Last active April 26, 2020 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjohnson/9eca4cf57052787ced517e0ff2e81b7e to your computer and use it in GitHub Desktop.
Save kjohnson/9eca4cf57052787ced517e0ff2e81b7e to your computer and use it in GitHub Desktop.
<?php
add_filter( 'ninja_forms_submit_data', function( $form_data ){
if( ! my_nf_validation( $form_data ) ) { // Add check here.
$errors = [
'fields' => [
'12' => __( 'An unexpected error occurred.', 'my-plugin' )
],
];
$response = [
'errors' => $errors,
];
echo wp_json_encode( $response );
wp_die(); // this is required to terminate immediately and return a proper response
}
// If no errors, be sure to return the $form_data.
return $form_data;
});
<?php
add_filter( 'ninja_forms_submit_data', function( $form_data ){
if( ! my_nf_validation( $form_data ) ) { // Add check here.
$errors = [
'form' => [
'my-error' => __( 'An unexpected error occurred.', 'my-plugin' ),
]
];
$response = [
'errors' => $errors,
];
echo wp_json_encode( $response );
wp_die(); // this is required to terminate immediately and return a proper response
}
// If no errors, be sure to return the $form_data.
return $form_data;
});
<?php
add_filter( 'ninja_forms_submit_data', function( $form_data ){
if( ! my_nf_validation( $form_data ) ) { // Add check here.
$errors = [
__( 'An unexpected error occurred.', 'my-plugin' )
];
$response = [
'errors' => $errors,
];
echo wp_json_encode( $response );
wp_die(); // this is required to terminate immediately and return a proper response
}
// If no errors, be sure to return the $form_data.
return $form_data;
});
<?php
// Form Error
$errors[ 'form' ][ $error_id ] = 'Form Error';
// Field Error
$errors[ 'fields' ][ $field_id ] = 'Field Error';
@farinspace
Copy link

Am I missing something, the "Submit" button continues to show "Processing" after the JSON returns. (NF 3.3.7)

@NadezdaG
Copy link

NadezdaG commented Nov 5, 2018

The same for me.

@NiceGuyNimni
Copy link

Same for me :(

@sewid
Copy link

sewid commented Dec 31, 2018

Same for me...

@wiesys
Copy link

wiesys commented Jan 12, 2019

The same was for me.

I found out, that you should return the response and without JSON encoding.
Also I was unable to display "form error", just "field error":

add_filter(  'ninja_forms_submit_data', 'my_ninja_forms_submit_data' ); 
function my_ninja_forms_submit_data( $form_data ) {
  $errors = [
    'fields' => [
      '5' => __( 'An unexpected error occurred.', 'my-plugin' ),
     ]
  ]; // '5' is field's id.
  $response = [
    'errors' => $errors,
  ];
  return $response;
}

@dfmmalaw
Copy link

Does anyone else get "Call to undefined function my_nf_validation()" error message?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment