• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

AAC Validator.php and CreateCharacter.php

bpm91

Advanced OT User
Joined
May 23, 2019
Messages
1,046
Solutions
7
Reaction score
180
Location
Brazil
YouTube
caruniawikibr
I’m trying to make it so that when creating or changing a name, the rules mentioned in validator.php are enforced, but as far as I understand, the validator only sends a message for the player to see. It doesn’t block the action. How can I block and prevent the creation or change of the name?
myaac - tfs 1.5

PHP:
`/**
     * Validate character name.
     * Name lenght must be 4-26 chars
     *
     * @param  string $name Name to check
     * @return bool Is name valid?
     */
    public static function characterName($name)
    {
        if(!isset($name[0]))
        {
            self::$lastError = 'Please enter character name.';
            return false;
        }

        $length = strlen($name);
        if($length < 4)
        {
            self::$lastError = 'Character name is too short. Min. length <b>4</b> characters.';
            return false;
        }

        if($length > 26)
        {
            self::$lastError = 'Character name is too long. Max. length <b>26</b> characters.';
            return false;
        }

        if(strspn($name, "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM- [ ] '") != $length)
        {
            self::$lastError = "Invalid name format. Use only A-Z, spaces and '.";
            return false;
        }

        if(preg_match('/ {2,}/', $name))
        {
            self::$lastError = 'Invalid character name format. Use only A-Z and numbers 0-9 and no double spaces.';
            return false;
        }

        if(!preg_match("/[A-z ']/", $name))
        {
            self::$lastError = "Invalid name format. Use only A-Z, spaces and '.";
            return false;
        }
        
        if (preg_match('/[^a-zA-Z0-9 \'-]/', $name))
        {
            self::$lastError = "Invalid name format. Use only A-Z, spaces, and single quotes (').";
            return false;
        }
        
        if (substr_count($name, "'") > 1)
        {
            self::$lastError = "Invalid name format. Only one single quote (') is allowed.";
            return false;
        }
        
        if (substr($name, 0, 1) == "'")
        {
            self::$lastError = "Invalid name format. Single quote (') cannot be at the beginning.";
            return false;
        }
        
        if (substr($name, -1) == "'")
        {
            self::$lastError = "Invalid name format. Single quote (') cannot be at the end.";
            return false;
        }
        $quotePosition = strpos($name, "'");
        if ($quotePosition !== false && $quotePosition < 3)
        {
         self::$lastError = "Invalid name format. Single quote (') can only appear from the third character onward.";
         return false;
        }

        return true;
    }`


createcharacter.php
`class CreateCharacter
{
    /**
     * @param $name
     * @param $errors
     * @return bool
     */
    public function checkName($name, &$errors)
    {
        $minLength = config('character_name_min_length');
        $maxLength = config('character_name_max_length');

        if(empty($name)) {
            $errors['name'] = 'Please enter a name for your character!';
            return false;
        }

        if(strlen($name) > $maxLength) {
            $errors['name'] = 'Name is too long. Max. length <b>' . $maxLength . '</b> letters.';
            return false;
        }

        if(strlen($name) < $minLength) {
            $errors['name'] = 'Name is too short. Min. length <b>' . $minLength . '</b> letters.';
            return false;
        }

        $name_length = strlen($name);
        if(strspn($name, "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM- '") != $name_length) {
            $errors['name'] = 'This name contains invalid letters, words or format. Please use only a-Z, - , \' and space.';
            return false;
        }

        if(!preg_match("/[A-z ']/", $name)) {
            $errors['name'] = 'Your name contains illegal characters.';
            return false;
        }

        if(!admin() && !Validator::newCharacterName($name)) {
            $errors['name'] = Validator::getLastError();
            return false;
        }

        $player = new OTS_Player();
        $player->find($name);
        if($player->isLoaded()) {
            $errors['name'] = 'Character with this name already exist.';
            return false;
        }

        return empty($errors);
    }

`
Post automatically merged:

I created several rules to not accept names like that but it doesn't work. Does anyone have any idea what I did wrong?

001.webp002.webp
 

Attachments

Last edited:

Similar threads

Back
Top