Last active
April 21, 2017 08:17
-
-
Save hell03610/e453f68748cb946bc0885a5006c356c0 to your computer and use it in GitHub Desktop.
ember-cp-validations
This file contains hidden or 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'; | |
const { | |
computed, | |
defineProperty | |
} = Ember; | |
export default Ember.Component.extend({ | |
classNames: ['validated-input'], | |
classNameBindings: ['showErrorClass:has-error', 'isValid:has-success'], | |
model: null, | |
value: null, | |
type: 'text', | |
valuePath: '', | |
placeholder: '', | |
validation: null, | |
showValidations: false, | |
didValidate: false, | |
notValidating: computed.not('validation.isValidating').readOnly(), | |
hasContent: computed.notEmpty('value').readOnly(), | |
hasWarnings: computed.notEmpty('validation.warnings').readOnly(), | |
isValid: computed.and('hasContent', 'validation.isTruelyValid').readOnly(), | |
shouldDisplayValidations: computed.or('showValidations', 'didValidate', 'hasContent').readOnly(), | |
showErrorClass: computed.and('notValidating', 'showErrorMessage', 'hasContent', 'validation').readOnly(), | |
showErrorMessage: computed.and('shouldDisplayValidations', 'validation.isInvalid').readOnly(), | |
showWarningMessage: computed.and('shouldDisplayValidations', 'hasWarnings', 'isValid').readOnly(), | |
init() { | |
this._super(...arguments); | |
let valuePath = this.get('valuePath'); | |
defineProperty(this, 'validation', computed.readOnly(`model.validations.attrs.${valuePath}`)); | |
defineProperty(this, 'value', computed.alias(`model.${valuePath}`)); | |
}, | |
focusOut() { | |
this._super(...arguments); | |
this.set('showValidations', true); | |
}, | |
input() { | |
this._super(...arguments); | |
var rawValue = this.get('value') || ''; | |
var containsUppercase = rawValue.indexOf('uppercase') > -1; | |
var sanitizedValue = rawValue.toUpperCase(); | |
if(containsUppercase) { | |
this.get('model').validateAttribute(this.get('valuePath'), sanitizedValue) | |
.then(({ m, validations }) => { | |
this.set('value', sanitizedValue); | |
}); | |
} | |
} | |
}); |
This file contains hidden or 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' | |
}); |
This file contains hidden or 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'; | |
import DS from 'ember-data'; | |
import { validator, buildValidations } from 'ember-cp-validations'; | |
const { computed } = Ember; | |
const { attr } = DS; | |
const Validations = buildValidations({ | |
firstName: validator('presence', true), | |
lastName: validator('presence', true), | |
dob: { | |
description: 'Date of birth', | |
validators: [ | |
validator('presence', true) | |
] | |
}, | |
phone: [ | |
validator('format', { | |
allowBlank: true, | |
type: 'phone' | |
}) | |
], | |
url: [ | |
validator('format', { | |
allowBlank: true, | |
type: 'url' | |
}) | |
] | |
}, { | |
debounce: 500 | |
}); | |
export default DS.Model.extend(Validations, { | |
firstName: attr('string'), | |
lastName: attr('string'), | |
dob: attr('date'), | |
phone: attr('string'), | |
url: attr('string') | |
}); |
This file contains hidden or 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 DS from 'ember-data'; | |
import { validator, buildValidations } from 'ember-cp-validations'; | |
const { attr } = DS; | |
const Validations = buildValidations({ | |
username: { | |
description: 'Username', | |
validators: [ | |
validator('presence', true), | |
validator('length', { | |
min: 5, | |
max: 15 | |
}) | |
] | |
}, | |
password: { | |
description: 'Password', | |
validators: [ | |
validator('presence', true), | |
validator('length', { | |
min: 4, | |
max: 10 | |
}), | |
validator('format', { | |
regex: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{4,10}$/, | |
message: '{description} must include at least one upper case letter, one lower case letter, and a number' | |
}), | |
validator('length', { | |
isWarning: true, | |
min: 6, | |
message: 'What kind of weak password is that?' | |
}) | |
] | |
}, | |
email: { | |
validators: [ | |
validator('presence', true), | |
validator('format', { | |
type: 'email' | |
}) | |
] | |
}, | |
emailConfirmation: validator('confirmation', { | |
on: 'email', | |
message: 'Email addresses do not match' | |
}), | |
details: validator('belongs-to') | |
}, { | |
debounce: 500 | |
}); | |
export default DS.Model.extend(Validations, { | |
username: attr('string'), | |
password: attr('string'), | |
email: attr('string'), | |
details: DS.belongsTo('user-detail') | |
}); |
This file contains hidden or 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.Route.extend({ | |
model() { | |
var details = this.store.createRecord('user-detail'); | |
var user = this.store.createRecord('user', {details: details}); | |
return user; | |
} | |
}); |
This file contains hidden or 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.3.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment