Series: Form Validations with ember-validations

Conditional Validations

Published on Aug 21, 2015

You don’t want all your validations to run all the time. Luckily, ember-validations has conditional statements that allow you to turn them on or off based on what’s going on in the app when it loads.

We go over the basic setup, the gotchas, and some of your options.


Links

Code

You can set a validation to trigger only if a certain condition is true:

queryParams: ['specialScrutiny'],
specialScrutiny: false,
validations: {
  species: {
    inclusion: {
      'if': 'specialScrutiny',
      in: ['Human', 'Homo Sapiens', 'Person'],
      message: 'Be more specific'
    }
  }
}

You can also use unless to do the opposite of if.

Transcript

Hey. Today we’re going to continue our discussion of the ember-validations library. We’re going to look at conditional validations.

So let’s say that our client figured out that he was being a little bit paranoid, so when he added his next validation, it’ll only be for ones that require special scrutiny. Let’s take a closer look at what’s happening. So we’re using the inclusion validation which has two different APIs, one of which we use up here, and one of which we use here. We’re going to be using this API. So we have the group that can be in, and then the message if it’s not in that group. We’ve also added an if statement. So it’s wrapped in quotes like this because of an ie8 bug, and then specialScrutiny is the name of a property that our validation will check to see if it’s true. So let’s go ahead and add that property. It’ll be specialScrutiny, and we’ll have it default to false. And of course if we check right now, it won’t say anything because it’s false. There’s nothing else. But if we set it to true, then it’s not in the specified list, so anything except one of the accepted values will give an error.

One thing to note is that this if statement, it doesn’t update if the property updates, so we can take specialScrutiny and we can say that it’s false. And it doesn’t change this. It’s still looking for the validation, even though specialScrutiny is now false. So that means that this if statement is best used when you’re passing in a value and that value is not going to change. One way to do this is in a component, and you want to use a component if you want to have validations during one type of use of this component, but have no validations during the use of the other type. That’s a really good use case. Another use case is doing queryParams. So if you have queryParams like this, then you can pass in that value to your controller.

There are also a couple of other options available to you when you’re using conditional validators. One is to use a function instead of just a property name. The other is to use unless instead of if, and it’ll work about as you expect.

So for our conditional validators, it’s a really useful tool, but just keep in mind that it doesn’t update when you update the value that it’s computing on. And that’s conditional validations. I hope you join us next week when we go over custom validations, both reusable ones and inline ones.

Form Validations with ember-validations

Subscribe to our mailing list