Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only validate properties on the model that exist in the form #245

Open
Webdesignwill opened this issue Oct 1, 2014 · 2 comments
Open

Only validate properties on the model that exist in the form #245

Webdesignwill opened this issue Oct 1, 2014 · 2 comments
Labels

Comments

@Webdesignwill
Copy link

It would be useful to validate only the fields that exist in the form for instance, if I want to validate on one form against the user model with the properties : username and email but I also have validation rules on the user model the correspond to phone number and address for instance, the model is never vaild when it's just validated against the username and email as it also checks for phone number and address.

I don't want to create a unique model for each form however, I just want to validate against the single model but only the specified fields.

You can do model.isValid(['email', 'username']); but this only returns true or false whereas model.validate() or model.set(props, {validate : true}); will validate the entire model including properties that don't exist in the form.

I propose that this module only validates against a model if the names are in the form or that isValid() also returns the corresponding error message.

@Webdesignwill
Copy link
Author

I have a solution that works pretty well and only modifies the existing code a small amount.
The user can call the validate method on the model and pass an option changedAttrs : true.

For example : this.set(user, {validate : true, changedAttrs : true});

Then in the backbone-validation.js file in the validate mixin on line 223, the result variable (line 233) can be set by checking for the changedAttrs option, if it exists, then it only validates the changed attributes or the ones sent in the form.

For example : result = validateModel(model, opt.changedAttrs ? changedAttrs : allAttrs);

That way I can choose to validate just the form components without having to make a model for each form and also if I want, I can validate the entire model if I ever need to.

@chiefGui
Copy link
Collaborator

We are planning to build something to read fields of a form and validate the model based on they — is this behavior are you expecting?

If so, we are discussing the same thing on #244 — please, enter the conversation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants