powmedia / backbone-forms

Form framework for BackboneJS with nested forms, editable lists and validation
MIT License
2.17k stars 413 forks source link

Help templates using form values #408

Open iabw opened 10 years ago

iabw commented 10 years ago

I've been using help templates that require other fields' values. These templates are evaluated using form.getValue() when the user hovers over an input ( as my help fields are styled as tooltips ). For an average form, it would probably perform well enough to evaluate on all 'change' events ( and the option should be turned off by default anyway ).

Is this a feature that could be a useful option in the core library (ie, should I write it into Form / Field class and file a pull request), or is it better suited to a plugin?

It seems like the most basic implementation wouldn't be much more than inserting these 2 code blocks into

Form https://github.com/powmedia/backbone-forms/blob/master/src/form.js#L79

if ( options.helpTemplates ){
  this.listenTo( this, "change", function(){
    var formValues = this.getValues();
    _.each( fields, function(field){
      field.evaluateHelpTemplate( formValues );
    });
  });
}

Field

evaluateHelpTemplate: function( formValues ){
  //helpClassName should be a setting with a default, same as errorClassName
  this.$( this.helpClassName ).replaceWith( _.template( this.help, formValues ) );
}
glenpike commented 8 years ago

Probably better to mixin this functionality - I think we did some mixing in with functionality at work, but need to check before putting example in.