Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Default values #16

Closed
tamtakoe opened this issue Dec 26, 2014 · 8 comments
Closed

Default values #16

tamtakoe opened this issue Dec 26, 2014 · 8 comments

Comments

@tamtakoe
Copy link

Often I need to set custom message (or other fields) for all validators. Now I have to write every time:

myField1: {
    email: {
        message: 'invalidEmail'
    }
}
...

myField2: {
    email: {
        message: 'invalidEmail'
    }
}
...

It is better:

validators.defaults.email.message = 'invalidEmail';
...

myField1: {
    email: true
}
...

myField2: {
    email: true
}
@ansman
Copy link
Owner

ansman commented Dec 26, 2014

Great idea! I'll make sure to implement this when I'm back from my vacation

On Fri, Dec 26, 2014 at 6:08 PM, tamtakoe notifications@github.com
wrote:

Often I need to set custom message (or other fields) for all validators. Now I have to write every time:

myField1: {
    email: {
        message: 'invalidEmail'
    }
}
...
myField2: {
    email: {
        message: 'invalidEmail'
    }
}
...

It is better:

validators.defaults.email.message = 'invalidEmail';
...
myField1: {
    email: true
}
...
myField2: {
    email: true
}

Reply to this email directly or view it on GitHub:
#16

@ansman
Copy link
Owner

ansman commented Feb 1, 2015

Pushed an implementation of this in d06bd13

Please have a look and see what you think, you can just close the issue if you're satisfied :)

@tamtakoe
Copy link
Author

tamtakoe commented Feb 2, 2015

Good! But it only for error params, not for options (f.e. dateOnly). There is best practice if you make object with default options (f.e. defaults) and merge it with options.

Using lodash defaults:

var defaults = {
    presence: ...
    ...
}
validators.defaults = defaults; //user can change it

...
//presence validator
_.defaults(options, defaults.presence);

@Jokero
Copy link
Contributor

Jokero commented Feb 2, 2015

I think it should be possible to set default value of any validator option. So I agree with @tamtakoe 👍

@ansman
Copy link
Owner

ansman commented Feb 5, 2015

Sure, sounds like a resonable feature request.

@ansman ansman closed this as completed in 1b65319 Feb 5, 2015
@ansman
Copy link
Owner

ansman commented Feb 5, 2015

Done, now you should be able to specify both default messages and default options. Please try it out and let me know what you think.

@Jokero
Copy link
Contributor

Jokero commented Feb 6, 2015

Thanks! 👍 I tried to use default messages and it's working.
Added comment 1b65319#commitcomment-9617489.
See please #10 (comment) (it's about promises)

@tamtakoe
Copy link
Author

tamtakoe commented Feb 6, 2015

It works! it may be rename options to more popular defaults
https://github.com/twbs/bootstrap/blob/master/js/popover.js#L24
http://docs.ractivejs.org/latest/ractive-defaults
http://stackoverflow.com/questions/8905507/jquery-plugin-defaults-and-options
But it is not critical.

Thank you for your work!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants