backbone.validation icon indicating copy to clipboard operation
backbone.validation copied to clipboard

Two different forms, one model

Open wilson29thid opened this issue 11 years ago • 10 comments

The other questions that are similar to this one don't seem to address this; forgive me if I'm misunderstanding.

If I have a User model and a login view as well as a signup view, how do I make password2 required on the signup view but not on the login view? My model's validation object has password2 set to required: true and it appears to be applying this to both views, even if the login view doesn't have a password2 field to begin with.

I'm binding via Backbone.Validation.bind(this); in the view's initialize() method.

wilson29thid avatar Sep 26 '14 01:09 wilson29thid

Same as https://github.com/thedersen/backbone.validation/issues/205. Lets keep talking there, please.

chiefGui avatar Jan 14 '15 16:01 chiefGui

I opened this issue again due to a wrong understanding of it. Anyway, I'm taking care of your feature man — it makes sense a lot.

chiefGui avatar Jan 14 '15 22:01 chiefGui

To validate a subset of the attributes is necessary to pass an array to isValid function:

model.isValid(['name', 'password2']) model.isValid(['name'])

What can be implemented is e.g. an option to be passed when bind is called specifying what attributes to be validated

blikblum avatar Jan 14 '15 23:01 blikblum

@blikblum It's time to automate this process. We'll pass an option to check the form based on its fields and then we validate what's within.

chiefGui avatar Jan 15 '15 12:01 chiefGui

I think this feature could be implemented in two steps, so it can be flexible and non invasive:

  1. Allow to pass custom attributes per binding. See WIP at #259
  2. Create a default function that would return the attributes used in the form. Something similar (simpler) to https://github.com/adjohnson916/backbone.stickit.form .

It's important to keep things not coupled because there's no uniform way to store/retrieve attribute names in forms.

This schema would allow, e.g., have a function that gets the attributes from view.validationAttrs field other that reads from the form using input name attribute and a third one that uses an RegExp pattern

blikblum avatar Jan 15 '15 15:01 blikblum

@blikblum I see your proposal being implicit and explicit — which is the same thing I thought. Implicit when there's something in the form our users don't want to validate and explicit when the entire form, according to the right field names, should be validated. :+1:

We should to go further discussing how we'll design these solutions. Please, let's talk faster on Gitter.

chiefGui avatar Jan 15 '15 16:01 chiefGui

I +1 this feature. We have a very similar requirement in our system where the same model in a given single page application might need different validations depending on where it is being loaded in the flow. We had to fork the repo and ended up making a _validations object on the model that is a map of validations given some key (because the same single page app can load different validations on the given model). If a key was set, and _validations existed, we loaded that set over the standard model.validation, otherwise we loaded the default.

SomethingSexy avatar Apr 02 '15 21:04 SomethingSexy

@SomethingSexy Just wondering, would setting validation to be a function (and changing this library to accept that) also work? You could use properties on the model (or anything else accessible from the model instance) to determine which set of validation rules to return.

platinumazure avatar Apr 03 '15 16:04 platinumazure

@platinumazure we thought about that but for our case (which might be very specific to us) we had to determine the configuration from outside of the model. We have some components that can be reused with the same view and same model instance but need to load different configurations at run-time.

SomethingSexy avatar Apr 03 '15 16:04 SomethingSexy

I am wondering if it possible to pass in your validation sets optionally instead of always pulling them from the model itself? When you are calling preValidate, isValid or validate, you could optionally pass in your validation.

SomethingSexy avatar Apr 03 '15 17:04 SomethingSexy