XF 2.2 How can I validate the User name field during registration with custom code in my add-on?

cowgod

Member
I want to write an add-on that verifies if the given User name during registration is a valid Minecraft username. I already know how to use the Minecraft/Mojang API, I'm just not sure how to hook into the XenForo registration form submit for my add-on. Can someone help me get started or at least point me to a current add-on that does something similar so I can look at the code?

I found an old add-on called XenForo-Minecraft-Username-Check but the source code was removed from GitHub.
 

Jeremy P

XenForo developer
Staff member
I would probably look into extending the \XF\Validator\Username class, which is responsible for determining if a given username is valid.
 

cowgod

Member
Thanks for the help. It doesn't appear to be working. I created my addon called MyAddon/Registration and I created the following file:

src/addon/MyAddon/XF/Validator/Username.php

PHP:
<?php

namespace MyAddon\Registration\XF\Validator

class Username extend XFCP_Username
{
        public function isValid($value, &$errorKey = null)
        {
                $isValid = parent::isValid($value, &$errorKey = null);

                $username = $value;

                if($isValid) {
                        if($username != 'test') {
                                $errorKey = 'censored';
                                return false;
                        }
                }

                return $isValid;
        }
}

I assumed that if I entered a username that was not "test" it would throw the "censored" error, but it didn't work and I was able to register.
 

cowgod

Member
I noticed I put "extend" instead of "extends". I also changed the if check to use !==. Neither of those seemed to fix the issue. Do I have to increment the version number if I change a php file in my src/addons/MyAddon directory to somehow get it to reload?
 

Snog

Well-known member
I noticed I put "extend" instead of "extends". I also changed the if check to use !==. Neither of those seemed to fix the issue. Do I have to increment the version number if I change a php file in my src/addons/MyAddon directory to somehow get it to reload?
This code works for me, no problem.

Code:
<?php

namespace MyAddon\Registration\XF\Validator;

class Username extends XFCP_Username
{
    public function isValid($value, &$errorKey = null)
    {
        $username = $value;

        $isValid = parent::isValid($value, $errorKey);

        if ($username !== 'test')
        {
            $errorKey = 'censored';
            return false;
        }

        return $isValid;
    }

}

Did you register your class extension in Admin->Development->Class extensions?
 
Last edited:

cowgod

Member
That was the missing piece. Thank you.

How would I go about creating my own errorKey? I know how to create a new phrase, but would I have to basically copy/paste the entire getPrintableErrorValue function from XF\Validator\Username.php and add it to my own class extension, adding my new case?
 

cowgod

Member
Looks like this works:

PHP:
<?php

namespace MyAddon\Registration\XF\Validator;

class Username extends XFCP_Username
{
        public function isValid($value, &$errorKey = null)
        {
                $isValid = parent::isValid($value, $errorKey);

                $username = $value;

                if ($username !== 'test')
                {
                        $errorKey = 'invalid_minecraft_name';
                        return false;
                }

                return $isValid;
        }

        public function getPrintableErrorValue($errorKey)
        {
                $phrase = parent::getPrintableErrorValue($errorKey);

                if(!$phrase) {
                        if($errorKey === 'invalid_minecraft_name') {
                                return \XF::phrase('myaddon_registration_please_enter_valid_minecraft_name');
                        }
                }

                return $phrase;
        }
}
 
Top