backbone-forms icon indicating copy to clipboard operation
backbone-forms copied to clipboard

integration with backbone-validation

Open jakubwolny opened this issue 12 years ago • 5 comments
trafficstars

What do you think about validating models by some external plugin e.g. Backbone.validation? https://github.com/thedersen/backbone.validation

It seems that Backbone.validation has much mature and complex validators than backbone-forms and it also more flexible - you can use validation without forms.

jakubwolny avatar Nov 12 '13 17:11 jakubwolny

Could be a good idea, I'll try to look into this when I get a chance. You're welcome to submit a pull request but if you plan on doing so could you outline what changes would be required and how you might go about doing this first?

powmedia avatar Nov 15 '13 15:11 powmedia

I am not sure if I will have a time to do this but here is my plan:

  • remove completely Form.validators
  • (optional) sanitize user input before sending values to validation (e.g. trim)
  • execute model validation on form submit or on input blur
  • populate error messages to related inputs

jakubwolny avatar Nov 15 '13 20:11 jakubwolny

The main thing to consider is how we include the validation plugin dependency. It would be best if you didn't have to depend on it being installed as this creates friction in getting set up with backbone forms.

So we can include it but then what if the user already has it in their project (thinking from an environment that is not using AMD or CommonJS)

Or we could include the basic validation as is currently, and somehow fall back to the full validation plugin if it is installed. I think the very basic validation we have in place at the moment covers a lot of use cases, at least in terms I getting a project up and running quickly.

On 15 Nov 2013, at 20:32, Jakub Wolny [email protected] wrote:

I am not sure if I will have a time to do this but here is my plan:

remove completely Form.validators (optional) sanitize user input before sending values to validation (e.g. trim) execute model validation on form submit or on input blur populate error messages to related inputs — Reply to this email directly or view it on GitHub.

powmedia avatar Nov 16 '13 19:11 powmedia

@hamczu have you ever started giving this a shot?

0xgeert avatar Feb 14 '14 14:02 0xgeert

Just fiddled around a bit with backbone-validation integration and tried to integrate it in the least intrusive way possible.

jsfiddle based on the 'register-form-custom-template' example

This example is probably pretty naive (it does a bind to the view on init but doesn't do a debind, etc.), but shows the model validation working quite nicely, through backbone-validation,

This:

  • Leaves Form.validators completely operational. Including all form / model validators provided
  • Allows model validation by having a validators on the model exactly as advertised in Backbone.validator.
  • Note: field-validations defined using backbone-forms takes precedence over backbone-validation (defined using validators on the model)
  • Correctly populates error messages (since all backbone-validation does is extend the validate method on model, which backbone-forms already calls to format errors from)
  • no definition on when to call validation (onblur, onKeyup etc), this should be left to the implementation imho
  • no input sanitization.

What more would be needed for a tighter, more useful integration?

0xgeert avatar Feb 14 '14 16:02 0xgeert