Giter Club home page Giter Club logo

Comments (4)

mikeu avatar mikeu commented on August 17, 2024 1

@billdami isn't that exactly what the changes parameter in the example and twiddle above gives you?

from ember-changeset-validations.

mikeu avatar mikeu commented on August 17, 2024

@hugoruscitti did you make any progress on this issue? I am facing the problem right now, with two date fields that are collectively only valid if the first is earlier than the second. I've made some progress, by using the changes and content parameters passed into the custom validation routine. For example,

// validators/custom.js
export default function validateLessThanAttr(largerAttrName = '') {
  return (key, newValue, oldValue, changes, content) => {
    const largerValue = Ember.get(changes, largerAttrName) || Ember.get(content, largerAttrName);
    return parseInt(newValue) < parseInt(largerValue) || `Must be less than ${largerValue}`;
  }
}

Here's a twiddle showing it in action. If you leave the larger number at its default, or set it to something new, and then change the smaller number to be greater than or equal to that, a validation error will appear. The logic would be very similar for dates of course, but with parsing via moment first, or however else you are dealing with them.

There is still one main problem with it that I'd like to find a way to address: A validation error on the smaller number isn't cleared if the larger number is changed to make a valid combination.

For example, if larger is 10 and smaller is 15, then the validation error on smaller will say "Must be less than 10". If I then change larger to be 20, the error message will still be there, saying that smaller has to be less than the old value of 10. (This is another example of #152.)

from ember-changeset-validations.

billdami avatar billdami commented on August 17, 2024

I've run into this as well, it seems like its currently not possible to have a custom validator whose rules are dependent on the current values of other properties in the changeset (that have NOT yet been applied to the target object).

It seems like maybe in addition to the content param (which is the underlying target object), the validators should be receiving another param, the changeset?

from ember-changeset-validations.

billdami avatar billdami commented on August 17, 2024

@mikeu you know what you're absolutely right. :D When I was originally trying to use the changes object for this, I was incorrectly testing for a truthy value, when in my case I should be just seeing if the changes object contains the property, even if its false.

from ember-changeset-validations.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.