• Resolved Nicolas

    (@nchaccal)


    Hi, i want to do a custom validation to user_login field.

    The thing is my custom validation works only in the registration form. But when try to edit profile of user already registered, i received an error message from WordPress. In the debug mode, wordpress told me something like this:

    Undefined index: user_login in function.php

    Uncaught error type: Argument 1 passed to CiValidator::validate_ci() must be of the type string, null given, called in function.php

    This is my code:

    /*validate user login*/
    add_action('um_submit_form_errors_hook_','um_custom_validate_username', 999, 1);
    function um_custom_validate_username( $args ) {
    
    class CiValidator
    {
        /**
         * @param string $ci
         * @return bool
         */
        public function validate_ci( string $ci ) : bool
        {
            $ci = $this->clean_ci($ci);
            $validationDigit = $ci[-1];
            $ci = preg_replace('/[0-9]$/', '', $ci );
    
            return $validationDigit == $this->validation_digit( $ci );
        }
    
        /**
         * @param string $ci
         * @return string
         */
        public function clean_ci( string $ci ) : string
        {
            return preg_replace( '/\D/', '', $ci );
        }
    
        /**
         * @param string $ci
         * @return int
         */
        public function validation_digit( string $ci ) : int
        {
            $ci = $this->clean_ci( $ci );
    
            $ci = str_pad( $ci, 7, '0', STR_PAD_LEFT );
            $a = 0;
    
            $baseNumber = "2987634";
            for ( $i = 0; $i < 7; $i++ ) {
                $baseDigit = $baseNumber[ $i ];
                $ciDigit = $ci[ $i ];
    
                $a += ( intval($baseDigit ) * intval( $ciDigit ) ) % 10;
            }
    
            return $a % 10 == 0 ? 0 : 10 - $a % 10;
        }
    }
    	$validator = new CiValidator();
    
    	if ( isset( $args['user_login'] ) && !ctype_digit( $args['user_login'] ) || !$validator->validate_ci( $args['user_login'] ) ) {
    		UM()->form()->add_error( 'user_login', 'invalid document ID' );
    	}
    }

    Any help will be appreciated. I dont have good skills in php :/

    • This topic was modified 4 years, 3 months ago by Nicolas.
    • This topic was modified 4 years, 3 months ago by Nicolas.
    • This topic was modified 4 years, 3 months ago by Nicolas.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Nicolas

    (@nchaccal)

    I already understood what was happening

    In the login form, user_login field does not appear. Thats why says its null.

    • This reply was modified 4 years, 3 months ago by Nicolas.
    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @nchaccal

    Thanks for letting us know. I am closing this thread now.

    Regards,

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Problem with custom validation’ is closed to new replies.