validatorjs icon indicating copy to clipboard operation
validatorjs copied to clipboard

[Bug] Rules which use dot notation do not get given correct attribute names

Open OwenMelbz opened this issue 5 years ago • 1 comments

Hey,

We've got a data structure (simplified to this)

   const data = {
        name: "owen",
        address: {
            line_one: "xxx",
            line_two: null
        }
    }

    validator = new Validator(data, {
        "address.line_one": "required",
        "address.line_two": "required"
    })

The validation all kicks in perfectly, however the attribute name replacement doesn't quite work!

It ends up returning

    {
        "address.line_two": [
            "The address.line two field is required."
        ]
    }

As you can see the :attribute replacement ends up being address.line two - it has happily replaced the underscores.

However it's failed to remove anything before the . dot notation.

Thanks!

OwenMelbz avatar Apr 12 '19 15:04 OwenMelbz

This is still an issue. You can get around it while it gets fixed by setting a custom attributeFormatter on your validator:

  validator.setAttributeFormatter((attribute) => {
    // Replaces `._[` with a `{blank space}` to make the attribute a bit more presentable
    return attribute.replace(/[._[]/g, ' ').replace(/]/g, '')
  })

oferrero avatar Mar 12 '21 03:03 oferrero