There is a UserName validator which is a part of the Drupal's user module but that cannot be used. That constraint validator always requires a value but we don't need that. Also, the validator works with different typed data whereas we will work with just strings.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

hussainweb created an issue. See original summary.

junkuncz’s picture

What is the exact idea here?

prethiee’s picture

I'm guessing here is to add some custom constraints, validating the username. I used this user_validate_name function from D7 as reference except for requiring username and length, cause I think that the length can be added in the FieldType. Attaching a patch, please take a look into it and let me know if it fits the requirements you're going for.

prethiee’s picture

Status: Active » Needs review
prethiee’s picture

Assigned: Unassigned » prethiee
junkuncz’s picture

Hi @prethiee!

Your code looks good to me, and functionality is also fine.
The decision is up to @hussainweb to accept and commit is.

Cheers,

Bálint

hussainweb’s picture

Status: Needs review » Needs work

Thanks @prethiee for the patch. I have a few comments below. Apart from that, I assume you took the violations right from the UserName validator?

  1. +++ b/src/Plugin/Validation/Constraint/UsernameFormatConstraint.php
    @@ -0,0 +1,52 @@
    + *   id = "UsernameFormat",
    

    Let's namespace this id. How about calling it `DoUsernameFormat`?

  2. +++ b/src/Plugin/Validation/Constraint/UsernameFormatConstraint.php
    @@ -0,0 +1,52 @@
    +   public $multipleSpace = 'The username cannot contain multiple spaces in a row.';
    

    Extra space here

  3. +++ b/src/Plugin/Validation/Constraint/UsernameFormatConstraint.php
    @@ -0,0 +1,52 @@
    +    public $invalidCharacter = "The username contains punctuation, only periods, hyphens, apostrophes, and underscores are allowed.";
    

    Extra space here

  4. +++ b/src/Plugin/Validation/Constraint/UsernameFormatConstraint.php
    @@ -0,0 +1,52 @@
    +     public $illegalCharacter = "The username contains an illegal character.";
    

    Extra space here

  5. +++ b/src/Plugin/Validation/Constraint/UsernameFormatConstraintValidator.php
    @@ -0,0 +1,35 @@
    +      if (substr($item->value, 0, 1) == ' ') {
    

    The indentation is incorrect.

yonas.legesse’s picture

Status: Needs work » Needs review
FileSize
3.89 KB

Changed plugin identifier and checked for indentations.

prethiee’s picture

Thanks for the patch @yonas.legesse, yeah @hussainweb I used D7's user_validate_name function as reference.

  • hussainweb committed 950c768 on 8.x-1.x
    Issue #3077523 by prethiee, yonas.legesse, hussainweb: Add username...
hussainweb’s picture

Status: Needs review » Fixed

Thanks for all your work here.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.