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 ?
 
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.
 
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?
 
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.
 
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.webp

1610486320972.webp

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.
 
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.webp



* waits for OyuncularSehri to post an angry reaction ...
 
What can i do with a callback here?
Have done it some years before and forgotten.
Can i manipulate the input like:

input = "123 123 123"

input after callback with a regex to replace spaces: "123123123"
 
Back
Top Bottom