Support

Account

Home Forums Front-end Issues acf form validation only on certain page

Solved

acf form validation only on certain page

  • Hi i’m running a check if an email exists during signup, but only want this to run on the registration template, not my template for updating details.

    I’ve tried the below but doesn’t seem to work

    add_filter('acf/validate_value/name=sr_email', 'my_acf_validate_email', 10, 4);
    function my_acf_validate_email( $valid, $value, $field, $input ){
    	
    	if( !$valid ) {
    		return $valid;
    	}
    
    	if(is_page(483) && email_exists($value)){  //483 signup
    		$valid = 'Email already in use';
    	}
    
    	return $valid;
    }
  • Hi @sami616

    Could you please tell me if this “registration template” is on front end or back end?

    If it is on the front end, I think you can add a hidden input on your form to mark it as a registration page. Maybe something like this:

    acf_form(array(
        'html_after_fields' => '<input type="hidden" name="acf[register]" value="true"/>',
    ));

    After that, you can check this hidden input using the $_POST variable. It should be something like this:

    add_filter('acf/validate_value/name=sr_email', 'my_acf_validate_email', 10, 4);
    function my_acf_validate_email( $valid, $value, $field, $input ){
    	
    	if( !$valid ) {
    		return $valid;
    	}
        
    	if(($_POST['acf']['register'] == 'true') && email_exists($value)){
    		$valid = 'Email already in use';
    	}
    
    	return $valid;
    }

    I hope this helps.

  • Excellent, thanks James!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘acf form validation only on certain page’ is closed to new replies.