1

How would I implement a filter to block out spaces in username (during registration)?

I've been looking at the Fortress documentation, and probably the regex option is the one to look for. But there's no documentation examples on regex, and I've been unsuccessful so far in my own efforts (including \ escaping issues in a JSON document).

I hope this is something simple to resolve. Thank you.

[UserFrosting 0.3.1]

Sparky
  • 98,165
  • 25
  • 199
  • 285
amivag
  • 91
  • 4

1 Answers1

1

Regex would indeed be the best option in this case. Have you tried:

"user_name" : {
    "validators" : {
        "length" : {
            "min" : 1,
            "max" : 50,
            "message" : "ACCOUNT_USER_CHAR_LIMIT"
        },
        "required" : {
            "message" : "ACCOUNT_SPECIFY_USERNAME"
        },
        "regex" : {
            "regex" : "/^[\S]*$/",
            "message" : "Username cannot contain any whitespace characters."
        }
    },
    "sanitizers" : {
        "escape" : {}
    }        
}
alexw
  • 8,468
  • 6
  • 54
  • 86
  • Doesn't work, invalid JSON in error log and blank page as a result. I think it's the \ that needs to escaped somehow. Maybe something else. Also, you forgot a comma after "regex"? I also got "Undefined index: regex," in some of my tests. Could not get it to work. – amivag Sep 20 '16 at 21:26
  • Fixed the comma. So the error is only on the server side code? Or is it failing when it is translated into the Javascript rules as well? – alexw Sep 21 '16 at 02:31
  • For testing, I replaced the "user_name" entry in register.json, with your version. Register page produces a blank (white) document. In the server error log I get the following: "...register.json' could not be found, or it does not contain a valid JSON document...". The problem seems to be the \ (backslash) in the regex entry. Once I remove the backslash, the page loads. (but the script does not work as intended of course) – amivag Sep 23 '16 at 09:36
  • So it looks the problem is that the schema needs to be recognized by both Javascript and PHP as a valid JSON document. As you found, the `\\` is causing a problem. Apparently you need to use _four_ backslashes to escape properly - check out [this answer](http://stackoverflow.com/a/25426567/2970321). – alexw Sep 30 '16 at 22:47