Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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

This comment has been minimized.

Copy link

@Arriegi Arriegi commented Oct 31, 2017

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

@abhilashlegend

This comment has been minimized.

Copy link

@abhilashlegend abhilashlegend commented Apr 23, 2019

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