Skip to content

2. Validating Models

Duncan Walker edited this page Mar 7, 2016 · 1 revision

Ember Validations

Validations are run automatically using the Ember Validations library when you add a validations hash to your controller.

// app-name/components/fruits/new.js

import Ember from 'ember';
import FormMixin from 'ember-easy-form-extensions/mixins/components/form';

export default Ember.Component.extend(
  FormMixin, {

  validations: {
    'model.name': {
      presence: true
    }
  },

  /* save() only runs when the validations pass */

  save: function() {
    this.get('model').save().then(function(fruit) {
      this.transitionTo('fruit', fruit);
    });
  },

});

Custom Validations

You can run custom validations for model tests that aren't supported by Ember.Validations:

// app-name/components/fruits/new.js

import Ember from 'ember';
import FormMixin from 'ember-easy-form-extensions/mixins/components/form';

export default Ember.Component.extend(
  FormMixin, {

  runCustomValidations() {
    return new Ember.RSVP.Promise(function(resolve, reject) {
      /* Run some kind of validations Ember.Validations doesn't support

      resolve() to validate the model successfully
      reject() to validate the model unsuccessfully
      */

      if (this.get('someContent').mapBy('id').indexOf(5) > -1) {
        resolve(); // Continue
      } else {
        reject(); // Fail
      }
    });
  },

  save: function() {
    this.get('model').save().then(function(fruit) {
      this.transitionTo('fruit', fruit);
    });
  },

});

runCustomValidations must return a promise.

You can also use Ember Validations custom validator base class, which is documented here.

Conditional Validators

Conditional validators are supported by Ember Validations and are documented here. When you use conditional validators you may need to revalidate your model.

This addon provides a ConditionalValidationsMixin, which can be used as follows:

// app-name/components/fruits/new.js

import ConditionalValidationsMixin from 'ember-easy-form-extensions/mixins/components/conditional-validations';
import Ember from 'ember';
import FormMixin from 'ember-easy-form-extensions/mixins/components/form';

export default Ember.Component.extend(
  ConditionalValidationsMixin,
  FormMixin, {

  isFruit: true,
  revalidateFor: ['isFruit'], // Add property to array

  validations: {
    'model.name': {
      presence: {
        'if': function(object, validator) {
          return this.get('isFruit'); // Have to use a property here
        }
      }
    }
  },

  /* save() only runs when the validations pass */

  save: function() {
    this.get('model').save().then(function(fruit) {
      this.transitionTo('fruit', fruit);
    });
  },

});

onInvalidSubmission

If you want to perform something when a form is submitted but turns out to be invalid, simply add an onInvalidSubmission() method to the class with the form mixin:

// app-name/components/fruits/new.js

import Ember from 'ember';
import FormMixin from 'ember-easy-form-extensions/mixins/components/form';

export default Ember.Component.extend(
  FormMixin, {

  validations: {
    name: {
      presence: true,
    },
  },

  onInvalidSubmission() {
    Ember.warn(this.get('errors'));
    alert('Form is invalid');
  },

});