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

form.commit({validate:false}) still validates individual fields #540

Open
hcharley opened this issue Feb 7, 2017 · 6 comments
Open

form.commit({validate:false}) still validates individual fields #540

hcharley opened this issue Feb 7, 2017 · 6 comments

Comments

@hcharley
Copy link

hcharley commented Feb 7, 2017

When I call form.commit({validate: false}) I am receiving errors in response.

var MyModel = Backbone.Model.extend({
  schema: {
    headline: { type: "Text", validators: [ "required" ] },
    slug: { type: "Text", validators: [ "required" ] }
  }
});
var model_instance = new MyModel();
var form = new BackboneForm({
  model: model_instance,
  fields: [ "slug", "headline" ]
}).render();

$('body').append(form.el);
var result = form.commit({ validate: false });
console.log("number of errors: ", _.keys(result).length);

screen shot 2017-02-07 at 2 37 27 pm

It looks like individual fields are being validated with in form.validate() BEFORE checking if skipModelValidate is set.

I'm going to file a PR shortly to check skipModelValidate before the individual fields are validated.

This wasn't intentional, right? I'm not missing something important?

@philfreo
Copy link
Collaborator

philfreo commented Feb 7, 2017

From the README:

There are 2 levels of validation: schema validators and the regular built-in Backbone model validation. Backbone Forms will run both when form.validate() is called. Calling form.commit() will run schema level validation by default, and can also run model validation if { validate: true } is passed.

In other words, I believe the validate flag is specifically designed to mirror / pass through to Backbone's set (whose option is described on http://backbonejs.org/#Model-validate). So yes, I think it was intentional this way and it would definitely a backwards incompatible change for validate: false to suddenly also ignore schema-level validation.

If this behavior is really needed, perhaps introducing another option (e.g. schemaValidate) would be better, though I'd question why you bother with form/schema validation if you want to disable it.

@hcharley
Copy link
Author

hcharley commented Feb 7, 2017

@philfreo Thanks for breaking it down for me. I thought there was an intent that I wasn't picking up on.

My usecase is this:

I want to show users a rendered preview of their form values as they input data. Like a WYSIWG. (e.g. insert a headline in a form input, have it show up in a "preview pane" inside an <h1> tag).

I have a slug field that I definitely want to validate on form submission, but I don't care about whether or not it's valid before then. The slug doesn't show up in the preview pane.

So if the slug fails validation, I still want to be able to commit other values, like headline, to the model so that they get rendered on the preview. I don't want one invalid field to break my entire preview pane.

On form submit, I will definitely validate and prevent submission if there are invalid fields.

@philfreo
Copy link
Collaborator

philfreo commented Feb 7, 2017

One idea is to move slug validation to the Backbone model then. Or have a selective backbone-forms validator that only gets triggered when a certain criteria (final submission) is true.

@hcharley
Copy link
Author

hcharley commented Feb 8, 2017

I think I see what you're getting at. It's an anti-pattern to commit dirty data to the model, and in a sense defeats the purpose of having validation schemas in the first place. Perhaps, I am the only exception who would like to commit dirty data to my model.

However, I'm not a big fan of moving validation to the Backbone model's validate, as I lose out on backbone-form's schema pattern, which I really prefer, and ultimately on final submission, I want to use.

Also, moving to a custom validator would be alright, but in practice, I'll probably end up writing custom validators for all my fields and then would have to lay some more code on to determine the state of a commit (final submission vs. preview render). So I'd either have to abstract Backbone-Form's validators or rewrite them.

I'm going to submit a PR to see if there are thoughts on adding an option to form.commit().

@0xgeert
Copy link

0xgeert commented Feb 8, 2017 via email

@0xgeert
Copy link

0xgeert commented Feb 8, 2017 via email

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

No branches or pull requests

3 participants