validatorjs icon indicating copy to clipboard operation
validatorjs copied to clipboard

Validation message for array not working as expected

Open cnakh opened this issue 3 years ago • 1 comments

I am writing code to validate array of emails and phone numbers as follow

var rules = {
    'emails[]': 'required_without:phones[]|array',
    'emails[].*': 'required|email',
    'phones[]': 'required_without:emails[]|array',
    'phones[].*': 'required|digits_between:9,10',
};
var messages = {
    'required_without.emails[]': 'Email is required when no any phones is not present',
    'array.emails[]': 'Emails must be an array',
    'required.emails[].*': 'Email is required',
    'email.emails[].*': 'Email must be a valid email address',
    'required_without.phones[]': 'Phone is required when no any emails is not present',
    'array.phones[]': 'Phones must be an array',
    'required.phones[].*': 'Phone is required',
    'digits_between.phones[].*': 'Phone must be between :min and :max digits'
};

When process of validate the result as follow

{
    "emails[].0": ["The emails .0 field is required."],
    "emails[].1": ["The emails .1 field is required."],
    "emails[].2": ["The emails .2 field is required."],
    "emails[].3": ["The emails .3 field is required."]
}

The error message was not override even I set it in messages 'required.emails[].*': 'Email is required'

How can I solve this issue?

cnakh avatar Apr 11 '21 09:04 cnakh

@cnakh Would you be able to share a bit more code? I can see how you have defined the rules and messages but I am not sure how you are invoking the validator, and seeing how the incoming data appears, I can advise further.

mikeerickson avatar Apr 24 '21 17:04 mikeerickson