Skip to content

Instantly share code, notes, and snippets.

@rossjha
Created January 25, 2018 15:48
Show Gist options
  • Save rossjha/b42281b555a6620473f26b1e687cf1d6 to your computer and use it in GitHub Desktop.
Save rossjha/b42281b555a6620473f26b1e687cf1d6 to your computer and use it in GitHub Desktop.
cp validations
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..');
}
}
});
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')
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
<p>New User:</p>
First Name: {{input
value=user.firstName
focus-out=(action (mut showTitleError) true)
}}
<br/>
<br/>
Last Name: {{input
value=user.lastName
focus-out=(action (mut showTitleError) true)
}}
<br/>
<br/>
Email: {{input
value=user.email
focus-out=(action (mut showTitleError) true)
}}
<br/>
<br/>
<br/>
<button {{action "submit"}}> Submit </button>
{
"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