mikeerickson / validatorjs

A data validation library in JavaScript for the browser and Node.js, inspired by Laravel's Validator.
https://www.npmjs.com/package/validatorjs
MIT License
1.77k stars 280 forks source link

Global attribute names for language #198

Open alisherafat01 opened 7 years ago

alisherafat01 commented 7 years ago

I know how to set custom attribute names for an instance of validator but I want to know is it possible to set them globally? (i need something like below): var validator = require('validatorjs'); validator.useLang('fa'); validator.setAttributes('fa',{ age: 'سن', full_name: 'نام کامل', ... });

alisherafat01 commented 6 years ago

Any progress on this issue?

vict-shevchenko commented 6 years ago

Hi, I am not a library owner, so my opinion may be different and not committed to be the true one.

As I understand you need something like global .setAttributeNames. Here is what I think.

class Validator is responsible for creating a validator instance. And as it is a constructor it defines the properties and methods for instance and holds some generic data. Class Validator does not interact with your form and knows nothing about your form fields(and about your form at all), but instance (validator) does.

Following your request, you are asking to break an encapsulation - in my opinion, this does not look good from the architectural point of view. The current implementation allows keeping some separation or responsibilities and roles.

What do you think?

Btw, @alisherafat can you help with one rule translation into Persian('fa') language? it is a "hex" rule https://github.com/skaterdav85/validatorjs/blob/master/src/lang/fa.js Can you reply me with translation here?

alisherafat01 commented 6 years ago

Hi @vict-shevchenko Translation of hex rule for fa_IR is: hex: 'فیلد :attribute باید در فرمت مبنای ۱۶ باشد'

As answered herer Laravel validator has option to modify attributeNames for a specific language globally and there is nothing wrong with that and is good thing to implement here.

actually attributes:{} property should be modifiable here

mikeerickson commented 6 years ago

This has been tagged as a feature request and will consider this addition for a future release.

vict-shevchenko commented 6 years ago

Cool! Glad to know this. Thanks a lot for translation.