Backbone.JS ValidationError()

The Backbone.JS ValidationError() model is used to display an error, in case if a validation fails or after an invalid event is triggered.

Syntax:

Model.ValidationError 

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<title>Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type="text/javascript"></script>
<script type="text/javascript">
var X = Backbone.Model.extend({
defaults: {
student_name: 'Tom',
student_age: 10,
},
initialize : function(){
this.on("invalid",function(model,error){
document.write(error);
});
},
validate: function(attributes) {
if ( attributes.student_age < 10 ) {
return 'Enter the correct age.';
}
},
});
var Y = new X();
Y.on('invalid', function() {
this.arguments;
});
Y.set({ student_age : '9' }, { validate : true });
</script>Enter the correct age.
<title>Example</title> <script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type="text/javascript"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type="text/javascript"></script> <script type="text/javascript"> var X = Backbone.Model.extend({ defaults: { student_name: 'Tom', student_age: 10, }, initialize : function(){ this.on("invalid",function(model,error){ document.write(error); }); }, validate: function(attributes) { if ( attributes.student_age < 10 ) { return 'Enter the correct age.'; } }, }); var Y = new X(); Y.on('invalid', function() { this.arguments; }); Y.set({ student_age : '9' }, { validate : true }); </script>Enter the correct age.
 

  
Example  
  
  
  
  
  
Enter the correct age.  
  
  

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Enter the correct age.
Enter the correct age.
Enter the correct age.

Explanation:
Since we set the value of student_age less than 10, hence it returned a Validation Error message.