• Resolved wpblogwriter

    (@wpblogwriter)


    Hello,

    I am trying to have a custom validation for the mail address field. I would like that only subscribers of a special mail domain are allowed to subscribe.

    I have used your code example here:
    https://docs.ultimatemember.com/article/94-apply-custom-validation-to-a-field

    and modified it a bit:

    add_action(‘um_submit_form_errors_hook_’,’um_custom_validate_mail’, 999, 1);

    function um_custom_validate_mail( $args ) {

    global $ultimatemember;

    if( isset( $args[‘user_email’] ) && filter_var($args[‘user_email’], FILTER_VALIDATE_EMAIL) ){

    $explode_array = explode(“@”, $args[‘user_email’]);
    $domain = $explode_array[1];

    if( $domain != “<MAIL_DOMAIN>” ){
    $ultimatemember->form->add_error( ‘user_email’, ‘Your mail address must contain the domain <MAIL_DOMAIN>.’ );
    }
    }
    }
    Also I have set the parameter to um_custom_validate_mail in the form builder field settings under “custom action”.

    I have this error in the webserver log:

    2019/01/23 14:24:49 [error] 19924#19924: *353577 FastCGI sent in stderr: “PHP message: PHP Fatal error: Uncaught Error: Call to a member function add_error() on null in /var/www/wp-content/themes/my_theme/inc/custom-plugin-actions.php:15
    Stack trace:
    #0 /var/www/wp-includes/class-wp-hook.php(286): um_custom_validate_mail(Array)
    #1 /var/www/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)
    #2 /var/www/wp-includes/plugin.php(453): WP_Hook->do_action(Array)
    #3 /var/www/wp-content/plugins/ultimate-member/includes/core/um-actions-form.php(236): do_action(‘um_submit_form_…’, Array)
    #4 /var/www/wp-includes/class-wp-hook.php(286): um_submit_form_errors_hook(Array)
    #5 /var/www/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(”, Array)
    #6 /var/www/wp-includes/plugin.php(453): WP_Hook->do_action(Array)
    #7 /var/www” while reading response header from upstream, client: 5.15.5.15, server: domain.tld, request: “POST /register/ HTTP/2.0”, upstream: “fastcgi://unix:/var/lib/php7.0-fpm/web889.sock:”, host: “www.domain.tld”, referrer: “https://www.domain.tld/register/&#8221;

    I’d be happy for some info/help..

    Thank you

    • This topic was modified 5 years, 10 months ago by wpblogwriter.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The tutorial that you took this from is out dated. Ultimate Member 2.0 does not use the global variable $ultimatemember. Here is the updated code that should work

    <?php
    add_action('um_submit_form_errors_hook_','um_custom_validate_mail', 999, 1);
    
    function um_custom_validate_mail( $args ) {
    
    	if ( isset( $args['user_email'] ) && filter_var($args['v'], FILTER_VALIDATE_EMAIL) ) {
    
    		$explode_array = explode( '@', $args['user_email'] );
    		$domain = $explode_array[1];
    
    		if ( $domain != "<MAIL_DOMAIN>" ) {
    			UM()->form()->add_error( 'user_email', 'Your mail address must contain the domain <MAIL_DOMAIN>.' );
    		}
    	}
    }
    Thread Starter wpblogwriter

    (@wpblogwriter)

    Very nice! Thank you!

    I tried this by trial and error , and it worked. I couldn’t understand this code proeprly , please someone explain me .

    add_action('um_custom_field_validation_ff_exist','um_custom_field_validation_ff_exist', 10, 3);
    function um_custom_field_validation_ff_exist( $key, $array, $args ) 
    {
    	global $ultimatemember;  
    	
    	if ( (isset( $args['username'] ) && username_exists( $args['username'] ) == false && email_exists( $args['username'] ) == false )  ) 
    	{
    			$ultimatemember->classes['form']->add_error($key, 'No Freedom Fighter account has been found with this username or email');
    			//$ultimatemember->classes['form']->add_error($args['username'], __('Enter a valid 8-number serial please') );  
    			//$ultimatemember->classes['form']->add_error('ff_exist','No Freedom Fighter account has been found with this email 2');
    	}
    }
    

    I iserted this function in function.php of my theme . Then in register form , I put “ff_exist” in custom validation of ” Username or E-mail of Freedom Fighter ” , I found that metakey of this is “username” , hence I had used $args[‘username’] for checking .

    1. But I dont understand what is $key , and $array here ?
    2. why “//$ultimatemember->classes[‘form’]->add_error(‘ff_exist’,’No Freedom Fighter account has been found with this email 2′);” didn’t work it ? why I couldn’t use ” ff_exist ” instead of “$key” ?

    • This reply was modified 5 years, 9 months ago by mnhpias.
    • This reply was modified 5 years, 9 months ago by mnhpias.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom Validation for Subscription Form’ is closed to new replies.