backbone-forms
backbone-forms copied to clipboard
integration with backbone-validation
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.
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?
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
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.
@hamczu have you ever started giving this a shot?
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
validatorson the model exactly as advertised in Backbone.validator. - Note: field-validations defined using backbone-forms takes precedence over backbone-validation (defined using
validatorson the model) - Correctly populates error messages (since all backbone-validation does is extend the
validatemethod on model, which backbone-forms already calls to format errors from) - no definition on when to call validation (
onblur,onKeyupetc), this should be left to the implementation imho - no input sanitization.
What more would be needed for a tighter, more useful integration?