Created
January 25, 2018 15:48
-
-
Save rossjha/b42281b555a6620473f26b1e687cf1d6 to your computer and use it in GitHub Desktop.
cp validations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle', | |
user: null, | |
init(){ | |
this._super(); | |
const user = this.get('store').createRecord('user', {email: 'akram@'}); | |
this.set('user', user); | |
}, | |
actions: { | |
submit(){ | |
const user = this.get('user'); | |
user.validate().then(({validations}) => { | |
//this should be true :( | |
console.log('isWarning', validations.get('isWarning')); | |
//this is not working | |
const warnings = validations.get('warningMessages'); | |
console.log('warnings', warnings); | |
// whereas this works great | |
const attributeWarning = user.get('validations.attrs.email.warningMessages'); | |
console.log('attributeWarning', attributeWarning); | |
}); | |
console.log('submitting..'); | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Model from "ember-data/model"; | |
import attr from "ember-data/attr"; | |
import { belongsTo, hasMany } from "ember-data/relationships"; | |
import { validator, buildValidations } from 'ember-cp-validations'; | |
const Validations = buildValidations({ | |
firstName: validator('presence', { | |
presence: true, | |
message: 'first name can not be blank' | |
}), | |
lastName: validator('presence', { | |
presence: true, | |
message: 'last name can not be blank' | |
}), | |
email: [ | |
validator('presence', { | |
presence: true, | |
message: 'email can not be blank' | |
}), | |
validator('format', { | |
regex: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, | |
isWarning: true, | |
message: 'Oops, we need a vaild email address.' | |
}) | |
] | |
}); | |
export default Model.extend(Validations, { | |
email: attr('string'), | |
firstName: attr('string'), | |
lastName: attr('string') | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.12.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.12.0", | |
"ember-template-compiler": "2.12.0", | |
"ember-testing": "2.12.0" | |
}, | |
"addons": { | |
"ember-data": "2.12.1", | |
"ember-cp-validations": "3.4.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment