somnambulist-tech / validation

A re-write of rakit/validation, a standalone validation library inspired by Laravel Validation
MIT License
44 stars 13 forks source link

Custom message separator - ':' or '.'? #20

Closed jagdtiger9 closed 1 year ago

jagdtiger9 commented 1 year ago

It looks like custom message key separator is ':' class Validation { private AttributeBag $attributes; ... private string $separator = ':'; ... protected function resolveMessage(Attribute $attribute, Rule $rule, mixed $value): ErrorMessage { ... $messageKeys = [ $attributeKey . $this->separator . $ruleName, $attributeKey, $message->key(), ]; And doc uses '.' $factory->messages()->add('es', [ 'rule.required' => 'Se requiere :attribute', ]);

dave-redfern commented 1 year ago

If you wish to assign an error message to a specific field/attribute e.g. username, prefix with the attribute name and a : and then add the rule name (that uses dot separation). Otherwise if you set only on the rule name, then all errors will use that message.

For example: username => required|email|max:255, may want a specific error message: username:email => 'usernames require a valid email address'. That message would only be displayed against a field named username.

Does that help?

jagdtiger9 commented 1 year ago

Sry for bad exmaple code formatting And yeah, i read the doc inattentively, sry again \ TY very much!

dave-redfern commented 1 year ago

No worries! Glad to be of help. If you have any further questions, don't hesitate to open an issue.