XF 2.2 Regex Not Working

btmgreg

Well-known member
Hey all,

I've been trying to use the following template as a custom validator. The rest seem to be working fine, but I think this one is because the url has a hyphen inside like this -


Code:
<?php

namespace XF\Validator;

use function is_string;

class Linkedin extends AbstractValidator
{
    public function isValid($value, &$errorKey = null)
    {
        if (!preg_match('/^[a-z0-9_]+$/i', $value))
        {
            $errorKey = 'please_enter_valid_twitter_name_using_alphanumeric';
            return false;
        }

        return true;
    }

    public function coerceValue($value)
    {
        if (is_string($value) && $value && $value[0] == '@')
        {
            $value = substr($value, 1);
        }
        else if (preg_match('#linkedin\.com/in/(?P<id>[a-z0-9\_.]+)$#i', $value, $match))
        {
            $value = $match['id'];
        }

        return $value;
    }
}
I have tried adding a hyphen to this code with no results -

/(?P<id>[a-z0-9\_.]+)$#i', $value, $match))

I've tried a few variations online but had no luck. Anyone got any tips to handle a validator where the link has hyphens or other symbols other than alphanumeric?

Thank you :)
 
Try this linkedin\.com/in/(?P<id>[a-z0-9_\-.]+)/?$
use tools like https://regex101.com/ to test your regex
Hey bud. Thank you. It doesn't seem to work/change anything. Not sure if I've misunderstood.

File contents are now -

Code:
<?php

namespace XF\Validator;

use function is_string;

class Linkedin extends AbstractValidator
{
    public function isValid($value, &$errorKey = null)
    {
        if (!preg_match('/^[a-z0-9_]+$/i', $value))
        {
            $errorKey = 'please_enter_valid_twitter_name_using_alphanumeric';
            return false;
        }

        return true;
    }

    public function coerceValue($value)
    {
        if (is_string($value) && $value && $value[0] == '@')
        {
            $value = substr($value, 1);
        }
        else if (preg_match('#linkedin\.com/in/(?P<id>[a-z0-9_\-.]+)/?$#i', $value, $match))
        {
            $value = $match['id'];
        }

        return $value;
    }
}

1670183852824.webp

Ignore the 'Twitter' mention in the error. I've not changed that yet.

Appreciate you.
 
Looks like you also need to change the regex in isValid() because that doesn't include a hyphen as being a valid character.
Thanks. I thought the same, but when I do - I get an error

Code:
class Linkedin extends AbstractValidator
{
    public function isValid($value, &$errorKey = null)
    {
        if (!preg_match('/^[a-z0-9_]+$/i', $value))
        {
            $errorKey = 'please_enter_valid_twitter_name_using_alphanumeric';
            return false;
        }

^ Being the original.


Code:
class Linkedin extends AbstractValidator
{
    public function isValid($value, &$errorKey = null)
    {
        if (!preg_match('#linkedin\.com/in/(?P<id>[a-z0-9_\-.]+)/?$#i', $value))
        {
            $errorKey = 'please_enter_valid_twitter_name_using_alphanumeric';
            return false;

I changed it to match the code below - and I still get the following -

1670183852824-png.277724
 
It's probably failing because https:// isn't matching. Try adding https?:// to your regex.

You could also use (?:https?://)? in case someone uses a link that doesn't include the https:// part. Of course you'd then need to add it before adding the link to any publicly facing pages as a link.
 
It's probably failing because https:// isn't matching. Try adding https?:// to your regex.

You could also use (?:https?://)? in case someone uses a link that doesn't include the https:// part. Of course you'd then need to add it before adding the link to any publicly facing pages as a link.
Hey. Thanks for the reply bud.

If I try and enter just www.linkedin.com/in/greg-spinks-6957abab for example, rather than https://www.linkedin.com/in/greg-spinks-6957abab - I still get the same error. So with that in mind, I'm not sure if it is the http(s):// that is the problem. I just don't know what! :D

I think there's something I'm overlooking, I'm just not experienced enough to know what.
 
Hey. Thanks for the reply bud.

If I try and enter just www.linkedin.com/in/greg-spinks-6957abab for example, rather than https://www.linkedin.com/in/greg-spinks-6957abab - I still get the same error. So with that in mind, I'm not sure if it is the http(s):// that is the problem. I just don't know what! :D

I think there's something I'm overlooking, I'm just not experienced enough to know what.

Sorry, just realized that too. If you're still using the same regex you last posted, then www. isn't matched either. You'll need to add www\. in there too.
 
Thing is, that would be true if the ^ anchor were used to “match the start of the string” but that’s not there, the only thing that regex has is the LinkedIn stuff up to the end of the string.

At this point I’d plug it into Regex101.com and see what’s not matching.
 
Thing is, that would be true if the ^ anchor were used to “match the start of the string” but that’s not there, the only thing that regex has is the LinkedIn stuff up to the end of the string.

At this point I’d plug it into Regex101.com and see what’s not matching.
Must just be an error in the code then. The regex matches everything except the https://www. parts. I put it into that site yesterday. I just left off the www. part when I did it. It matched the LinkedIn/* though.
 
Ok, I think I see the problem now. Your isValid regex doesn't match the - in the ID.

PHP:
public function isValid($value, &$errorKey = null)
    {
        if (!preg_match('/^[a-z0-9_]+$/i', $value))

Try this instead:

PHP:
public function isValid($value, &$errorKey = null)
    {
        if (!preg_match('/^[a-z0-9_-]+$/i', $value))

Also, you don't really need this part in the coerceValue function:

PHP:
if (is_string($value) && $value && $value[0] == '@')
{
    $value = substr($value, 1);
}

That just removes the @ if someone types it in as part of their Twitter handle.

Here's what I think might work for you:

PHP:
<?php

namespace XF\Validator;

class Linkedin extends AbstractValidator
{
    public function isValid($value, &$errorKey = null)
    {
        if (!preg_match('/^[a-z0-9\_.-]+$/i', $value))
        {
            $errorKey = 'please_enter_valid_twitter_name_using_alphanumeric';
            return false;
        }

        return true;
    }

    public function coerceValue($value)
    {
        if (preg_match('#linkedin\.com/in/(?P<id>[a-z0-9\_.-]+)$#i', $value, $match))
        {
            $value = $match['id'];
        }

        return $value;
    }
}
 
Last edited:
Back
Top Bottom