Giter Club home page Giter Club logo

Comments (9)

dfreeman avatar dfreeman commented on August 17, 2024 5

I've been playing around with a pattern like this for conditional validations over the past couple days.

It seems to work really nicely with the exception of needing to explicitly revalidate dependent fields whenever a field involved in a condition changes — I noticed the existing confirmation validator has the same issue where it doesn't revalidate if the value of its on field changes.

@poteto Do you have any thoughts on what first-class support for dependent validations might look like?

from ember-changeset-validations.

wongpeiyi avatar wongpeiyi commented on August 17, 2024 4

I encountered this use case as well and came up with the below. It's not too pretty but I think it's straightforward and versatile:

// Validate presence only if underlying model isNew
{
  quantity: [
    function(key, newValue, oldValue, changes, content) {
      if (content.get('isNew')) {
        return validatePresence(true)(...arguments);
      }
      return true;
    }
  ]
}

You could of course make it dependent on a value in changes. Given the philosophy behind validators being "just functions", I think it should work pretty well?

from ember-changeset-validations.

dfreeman avatar dfreeman commented on August 17, 2024 2

@celicoo I think the goal is to be able to programmatically enable/disable validations based on the value of other fields in the changeset.

from ember-changeset-validations.

fpalluel avatar fpalluel commented on August 17, 2024 1

+1
It would ve great if we could enable or disable each validation rule on the fly.

from ember-changeset-validations.

mschinis avatar mschinis commented on August 17, 2024 1

Would be great if this was supported out of the box.
I'm currently building a multi-step form, and would like to validate certain fields at a time.

from ember-changeset-validations.

jaredgalanis avatar jaredgalanis commented on August 17, 2024

I am now setting the property to 0 if the condition is fulfilled which causes the above input not to be rendered. This is probably acceptable for this use case, but I wonder if there might circumstances in the future where the property should be left as null or undefined and not set to 0 (i.e. there might be another place where this property is tested for undefined/null), and where skipping the validator would be a preferable option.

from ember-changeset-validations.

poteto avatar poteto commented on August 17, 2024

@dfreeman To be honest I have not, but I like what your conditional validator does. Let me experiment a little more. In the meantime, if you would like to make your conditional validator a plugin to e-c-v that would be great :)

from ember-changeset-validations.

yandiro avatar yandiro commented on August 17, 2024

I encountered this use case as well and came up with the below. It's not too pretty but I think it's straightforward and versatile:

// Validate presence only if underlying model isNew
{
  quantity: [
    function(key, newValue, oldValue, changes, content) {
      if (content.get('isNew')) {
        return validatePresence(true)(...arguments);
      }
      return true;
    }
  ]
}

You could of course make it dependent on a value in changes. Given the philosophy behind validators being "just functions", I think it should work pretty well?

How can I do that using more than one validator? Something like this:

if (content.get('isNew')) {
  return [validatePresence(true)(...arguments), myCustomValidator()]
}

from ember-changeset-validations.

yandiro avatar yandiro commented on August 17, 2024
quantity: [
    function(key, newValue, oldValue, changes, content) {
      if (content.get('isNew')) {
        return validatePresence(true)(...arguments);
      }
      return true;
    }
  ]

The answer to my own question would be :

quantity: [
    function(key, newValue, oldValue, changes, content) {
      if (content.get('condition1')) {
        return validatePresence(true)(...arguments);
      }
      return true;
    },
    function(key, newValue, oldValue, changes, content) {
      if (content.get('condition2')) {
        return validateSomethingElse(true)(...arguments);
      }
      return true;
    }
  ]```

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.