Validation for Ember changeset - see tip 61

Tip

In the last tip, i talked about the awesome addon ember-changeset, there is a companion addon to add validations to your changeset.

Install the addon with
ember install ember-changeset-validations


Then you can wire up your model validator

{​{​dummy-form
    changeset=(changeset user EmployeeValidations)
    submit=(action "submit")
    rollback=(action "rollback")
}​}​


Then create your validator

// validations/employee.js
import {​
  validatePresence,
  validateLength,
  validateConfirmation,
  validateFormat
}​ from 'ember-changeset-validations/validators';

export default {​
  firstName: [
    validatePresence(true),
    validateLength({​ min: 4 }​)
  ],
  lastName: validatePresence(true),
  email: validateFormat({​ type: 'email' }​),
  password: validateLength({​ min: 8 }​),
  passwordConfirmation: validateConfirmation({​ on: 'password' }​)
}​;


Try it out and let me know how it works.  Do you have a better way to handle validations?

Credits & Links

Share A Tip

Email me at emad@emberdaily.tips if you have a good ember tip.
If you are enjoying my daily tips please spread the word with this one-click tweet
If you do NOT like the tips then please reply to this email and let me know how I can improve them.