Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format(), formatLabel() not being added to custom validator #98

Closed
jpaas opened this issue Jan 18, 2013 · 5 comments
Closed

format(), formatLabel() not being added to custom validator #98

jpaas opened this issue Jan 18, 2013 · 5 comments

Comments

@jpaas
Copy link

jpaas commented Jan 18, 2013

I have 2 custom validators and for some strange reason, one gets the this.format() and this.formatLabel() methods and the other doesn't.

  _.extend Backbone.Validation.validators,
    isDate: (value, attr, dateFormat, model)->
      return @format("{0} must be a valid date of the format {1}", @formatLabel(attr, model), dateFormat) if dateFormat && !(value instanceof Date)

    isIpAddress: (value, attr, customValue, model)->
      return @format("Not a valid {0}", @formatLabel(attr, model)) if !value.match(Backbone.Validation.patterns.isIpAddress)

isDate always gets them, isIpAddress never gets them. I've tried reordering them, renaming them, removing isDate, but no matter what I do isIpAddress never gets them. Any ideas?

@thedersen
Copy link
Owner

That's very strange... Can you post some code showing how you use them in a model?

@jpaas
Copy link
Author

jpaas commented Jan 23, 2013

My validation for the ip_address looks like this

  validation:
    ip_address: (value, attr, computedState)->
      if @is_master()
        if !value
          return "Need it"
        Backbone.Validation.validators.isIpAddress(value, attr, null, this)

BTW - I really have found this validation framework useful. So thanks for that. One of the things I found myself wishing for is conditional validation like Rails has. Without it, I had to implement the validation example above to make sure I only validate the format when some other condition is true. In this case whether it's a master.

@thedersen
Copy link
Owner

I think the issue lies in the last line, where you call the validators directly. It gets a different context without the format functions mixed in. I'll see if I can get this fixed in the next release.

You can have conditional validation (sort of) by specifying the required validator as a function.

validation: {
  ip_address: {
    required: function() {
      return this.isMaster();
    },
    isIpAddress: true
  }
}

Note that if it's not required (e.g isMaster returns false) ip_adress has to be empty or an valid one. Otherwise, it will fail.

Hope this helps!

@jpaas
Copy link
Author

jpaas commented Jan 26, 2013

Thanks Thomas, that's what I was looking for. I didn't realize it was possible since most of the example of using a function are for whole validator rather than a specific rule of a validator. Although I see now that there is a subtle example of this in the docs.

@thedersen
Copy link
Owner

Will be more explicit about it in the docs. Thanks!

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

No branches or pull requests

2 participants