Created
January 27, 2016 18:30
-
-
Save kjohnson/9928872827165848c7c0 to your computer and use it in GitHub Desktop.
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
// Create a new object for custom validation of a custom field. | |
var myCustomFieldController = Marionette.Object.extend( { | |
initialize: function() { | |
// On the Form Submission's field validaiton... | |
var submitChannel = Backbone.Radio.channel( 'submit' ); | |
this.listenTo( submitChannel, 'validate:field', this.validateRequired ); | |
// on the Field's model value change... | |
var fieldsChannel = Backbone.Radio.channel( 'fields' ); | |
this.listenTo( fieldsChannel, 'change:modelValue', this.validateRequired ); | |
}, | |
validateRequired: function( model ) { | |
// Only validate a specific fields type. | |
if( 'custom' != model.get( 'type' ) ) return; | |
// Only validate if the field is marked as required? | |
if( 0 == model.get( 'required' ) ) return; | |
// Check if Model has a value | |
if( model.get( 'value' ) ) { | |
// Remove Error from Model | |
Backbone.Radio.channel( 'fields' ).request( 'remove:error', model.get( 'id' ), 'custom-field-error' ); | |
} else { | |
// Add Error to Model | |
Backbone.Radio.channel( 'fields' ).request( 'add:error', model.get( 'id' ), 'custom-field-error', 'This is an error message' ); | |
} | |
} | |
}); | |
// On Document Ready... | |
jQuery( document ).ready( function( $ ) { | |
// Instantiate our custom field's controller, defined above. | |
new myCustomFieldController(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is not working and is giving error. Can you please provide proper instruction how to do this