Advertisement
adczk

Forminator - alternative required fileds - "this or that field mandatory"

Aug 16th, 2022
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.20 KB | None | 0 0
  1. <?php
  2.  
  3. add_filter( 'forminator_custom_form_submit_errors', function( $submit_errors, $form_id, $field_data_array ) {
  4.     if( $form_id != 1539 ) {
  5.         return $submit_errors;
  6.     }
  7.  
  8.     $field_names = array();
  9.  
  10.     // Change this to the message that you want to show.
  11.     $message = 'Please enter e-mail or phone.';
  12.    
  13.     foreach( $field_data_array as $key => $value ){
  14.         $field_names[] = $value['name'];
  15.     }
  16.  
  17.     if ( !in_array( 'email-1', $field_names ) && !in_array( 'phone-1', $field_names ) ) {
  18.         $submit_errors[]['submit'] = $message;
  19.     }
  20.  
  21.     return $submit_errors;
  22. },15,3);
  23.  
  24.  
  25. add_filter( 'forminator_custom_form_invalid_form_message', 'wpmudev_invalid_form_error_msg', 10, 2 );
  26. function wpmudev_invalid_form_error_msg( $invalid_form_message, $form_id ){
  27.     if( $form_id != 1539 ) {
  28.         return $invalid_form_message;
  29.     }
  30.  
  31.     $email_flag = false;
  32.     $phone_flag = false;
  33.  
  34.     if( isset( $_POST ) ){
  35.         $email = isset( $_POST['email-1'] ) ?  sanitize_text_field( $_POST['email-1'] ) : '';
  36.         $phone = isset( $_POST['phone-1'] ) ?  sanitize_text_field( $_POST['phone-1'] ) : '';
  37.  
  38.         if(  !$date && !$phone ){
  39.             $invalid_form_message = __( 'Please enter e-mail or phone.', 'forminator' );
  40.         }
  41.     }
  42.    
  43.     return $invalid_form_message;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement