Support

Account

Home Forums General Issues Validation bound to specific form only

Helping

Validation bound to specific form only

  • How do you make sure that validation only fires for a specific form ? I’ve tried to attach an ID to the form, and then validate the fields calling them with the ID, but it’s not working, my validation fires in the back-end on my options pages also ..

    My form:

    
    <?php
    
    acf_form_head();
    
    $avatar_group = get_field( 'sys_avatar_field_group', 'option' );
    $current_user = wp_get_current_user();
    
    $user_id = get_current_user_id();
    $user_acf_prefix = 'user_';
    $user_id_prefixed = $user_acf_prefix . $user_id;
    
    $user_info = get_userdata($user_id);
    $member_email = $user_info->user_email;
    
    $options = array(
    	'post_id' => 'user_'.$current_user->ID,
    	'field_groups' => array(group_5e880597057c0, $avatar_group),
    	'submit_value' => 'Update Profile'
    );
    
    ?>
    

    Validation :

    
    /** User Meta Validation ------------------------------------------------ **/
    
    add_action('acf/validate_save_post', 'validate_user_input_data');
    
    function validate_user_input_data() {
    
      $email = $_POST['acf']['field_5e888c0933f31'];
      $user_info = get_userdata($user_id);
      $current_id = get_current_user_id();
      $current_email = get_the_author_meta('user_email', $current_id);
    
      if ( empty($_POST['acf']['field_5e880c9b0e92d']) ) {
        acf_add_validation_error( 'acf[field_5e880c9b0e92d]', 'Name cannot be empty' );
      }
    
      if ( empty($_POST['acf']['field_5e880d0daa23b']) ) {
        acf_add_validation_error( 'acf[field_5e880d0daa23b]', 'Surname cannot be empty' );
      }
    
      if ( empty($_POST['acf']['field_5e880d3baa23c']) ) {
        acf_add_validation_error( 'acf[field_5e880d3baa23c]', 'Nickname cannot be empty' );
      }
    
      if ( empty($_POST['acf']['field_5e886a3273989']) ) {
        acf_add_validation_error( 'acf[field_5e886a3273989]', 'Display name cannot be empty' );
      }
    
      if ( empty($_POST['acf']['field_5e888c0933f31']) ) {
        acf_add_validation_error( 'acf[field_5e888c0933f31]', 'You need to specify an email address' );
      }
    
      if ( email_exists($email) && $email != $current_email ) {
        acf_add_validation_error( 'acf[field_5e888c0933f31]', 'The email address you entered is not available' );
      }
    
    }
    
  • If you only want the validation to run on the front end then add a check at the top of your filter

    
    if (is_admin()) {
      return;
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Validation bound to specific form only’ is closed to new replies.