XF 2.2 Validator or Regular Expression in custom fields?

optrex

Well-known member
I'm using invisible recaptcha v2 on my forum but from the 50 legitimate members I've had sign up over the last 2 days, I've got 45 rejected/spam and although they're automatically handled, I don't really want to have long member lists of non members.
I can't add a question challenge and have recaptcha according to the settings in setup-options-user registration, so I was thinking if this could be done as a required custom field, where only one answer is legitimate?

As an example a required preference question would be what colour is the sky where only the correct answer of blue will work.

I'm assuming I set this up as either a validator or a regular expression. Am I right and btw, what's the difference between the 2 ?
 

Brogan

XenForo moderator
Staff member
Why not just use Q&A captcha?
It will achieve the same result.

As for the "long member lists of non members", set up the registration options to reject accounts which trip the spam settings.

You can periodically use the batch update threads function to delete them, although that's not recommended as it means they can just register again.
 

optrex

Well-known member
Why not just use Q&A captcha?
It will achieve the same result.
Because in the section it said use recaptcha for guests. I wanted to do the Q&A challenge for registrations but if there was another section of the site where guests were challenged I wanted it to be invisible.

Can you explain what a validator or a regular expression does please?
 

Brogan

XenForo moderator
Staff member
Regex is a search pattern which limits the input to whatever the regex string defines - i.e. alpha numeric characters, numbers only, etc.

The validator option requires custom code - you would need a class such as XF\Validator\MyValidator.
 

Brogan

XenForo moderator
Staff member
You can achieve what you want quite simply but I don't know how effective it's going to be really.

Create the custom user field and set the options like so:
1610486332166.png

1610486320972.png

Then create the following file in XF\Validator\Spam.php:

PHP:
<?php

namespace XF\Validator;

class Spam extends AbstractValidator
{
    public function isValid($value, &$errorKey = null)
    {
        if ($value != 'blue')
        {
            $errorKey = 'Invalid entry';
            return false;
        }

        return true;
    }
}

You can expand the if check to allow for capital letters, or use regex, etc.
 

Brogan

XenForo moderator
Staff member
If you want members to able to answer Blue or blue, use this regex:

PHP:
<?php

namespace XF\Validator;

class Spam extends AbstractValidator
{
    public function isValid($value, &$errorKey = null)
    {
        if (!preg_match('/^(B|b)lue$/', $value))
        {
            $errorKey = 'Invalid entry';
            return false;
        }

        return true;
    }
}

However, for a simple use case such as that, I would just go with the regex option directly:

1610654543944.png



* waits for OyuncularSehri to post an angry reaction ...
 
Top