Skip to content

Instantly share code, notes, and snippets.

@kjohnson
Created January 27, 2016 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjohnson/9928872827165848c7c0 to your computer and use it in GitHub Desktop.
Save kjohnson/9928872827165848c7c0 to your computer and use it in GitHub Desktop.
// 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();
});
@Arriegi
Copy link

Arriegi commented Oct 31, 2017

My question is... where do I have to put this code in the ninja-forms plugin??

@abhilashlegend
Copy link

This code is not working and is giving error. Can you please provide proper instruction how to do this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment