Skip to content

Instantly share code, notes, and snippets.

@kstover
Created November 10, 2016 18:47
Show Gist options
  • Save kstover/5cd5e043313c837d02be9c96d3e7da5a to your computer and use it in GitHub Desktop.
Save kstover/5cd5e043313c837d02be9c96d3e7da5a to your computer and use it in GitHub Desktop.
Ninja Forms Three - After a form has loaded
// Create a new object for custom validation of a custom field.
var nameSpaceController = Marionette.Object.extend( {
initialize: function() {
this.listenTo( nfRadio.channel( 'form' ), 'render:view', this.doCustomStuff );
},
doCustomStuff: function( view ) {
var formModel = view.model; // formModel will be a Backbone model with all of our form data.
var formID = formModel.get( 'id' ); // We can use .get( 'setting' ) get get any of our form settings.
/*
* We can also update or modify fields now.
*
* If we wanted to change a field value, for instance, and we knew the field key we wanted to target, we'd do something like:
* var fieldModel = formModel.get( 'fields' ).findWhere( { key: 'your_key' } )
*
* If you want to get a field by ID, you'd use:
* var fieldModel = formModel.get( 'fields' ).get( '99' );
*
* Now that we have our field model, we can modify any of its settings.
*
* fieldModel.set( 'label', 'New Field Label' );
* fieldModel.set( 'value', 'New Value' );
* fieldModel.trigger( 'reRender' ); // Tells the field to re-draw itself.
*
*
* You can do this with regular jQuery stuff if you know the ID # of your field.
*
* jQuery( '#nf-field-99' ).val( 'New Text!' ).change();
*
* If you change something using a jQuery .val() method, you'll need to call a .change() on the element as well.
*
*/
},
});
// On Document Ready...
jQuery( document ).ready( function( $ ) {
// Instantiate our custom controller, defined above.
new nameSpaceController();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment