Created
November 11, 2013 05:18
-
-
Save achakravarty/7408287 to your computer and use it in GitHub Desktop.
Sample code for demonstrating jQuery validations with Ember.js
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
var App = Ember.Application.create(); | |
App.Router.map(function(){ | |
this.resource('user'); | |
}); | |
App.UserController = Ember.ObjectController.extend(Ember.Evented, { | |
actions: { | |
submit: function(){ | |
var validation = {isValid: false}; | |
this.trigger('validate', validation); | |
if(validation.isValid){ | |
//Validation passed. Do something here | |
} | |
} | |
} | |
}); | |
App.UserView = Ember.View.extend({ | |
didInsertElement: function(){ | |
this.get('controller').on('validate', $.proxy(function (validation) { | |
validation.isValid = this.validate(); | |
}, this)); | |
}, | |
validate: function(){ | |
var validator = $("#form").validate(validations.formValidations); | |
if(!validator.valid()){ | |
validator.focusInvalid(); | |
return false; | |
} | |
return true; | |
} | |
}); | |
var validations = function () { | |
var common = { | |
errorClass: 'help-block', | |
highlight: function (element, errorClass, validClass) { | |
$(element).parents('div.form-group').addClass('has-error'); | |
}, | |
unhighlight: function (element, errorClass, validClass) { | |
$(element).parents('div.form-group').removeClass('has-error'); | |
} | |
}; | |
var formValidations = { | |
rules: { | |
txtFirstName: { | |
required: true | |
}, | |
txtLastName:{ | |
required:true | |
} | |
}, | |
message:{ | |
txtFirstName: { | |
required: "First Name is required" | |
}, | |
txtLastName:{ | |
required:"Last Name is required" | |
} | |
}, | |
errorClass: common.errorClass, | |
highlight: common.highlight, | |
unhighlight: common.unhighlight, | |
errorPlacement: function (error, element) { | |
error.insertAfter(element); | |
} | |
}; | |
return { | |
formValidations : formValidations | |
} | |
}; |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
<link rel="stylesheet" type="text/css" href="styles/bootstrap.css"> | |
</head> | |
<body> | |
<script type="text/x-handlebars"> | |
{{outlet}} | |
</script> | |
<script type="text/x-handlebars" id="user"> | |
<form id="form"> | |
<div> | |
<label for="txtFirstName">First Name</label> | |
<input type="text" | |
id="txtFirstName" placeholder="Enter First Name"> | |
</div> | |
<div> | |
<label for="txtLastName">Last Name</label> | |
<input type="text" | |
id="txtLastName" placeholder="Enter First Name"> | |
</div> | |
<button {{action 'submit'}}>Submit</button> | |
</form> | |
</script> | |
<script type="text/javascript" src="scripts/jquery-1.9.1.js"></script> | |
<script type="text/javascript" src="scripts/handlebars-1.0.0.js"></script> | |
<script type="text/javascript" src="scripts/ember-1.1.2.js"></script> | |
<script type="text/javascript" src="scripts/app.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I meet error like: Uncaught TypeError: Object [object Object] has no method 'validate'
Could you help me please!
Thanks