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

Two different forms, one model #244

Open
wilson29thid opened this issue Sep 26, 2014 · 10 comments
Open

Two different forms, one model #244

wilson29thid opened this issue Sep 26, 2014 · 10 comments
Labels

Comments

@wilson29thid
Copy link

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.

@chiefGui
Copy link
Collaborator

Same as #205. Lets keep talking there, please.

@chiefGui
Copy link
Collaborator

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.

@blikblum
Copy link
Contributor

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

@chiefGui
Copy link
Collaborator

@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.

@blikblum
Copy link
Contributor

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 Allow to configure validation attributes per view #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

@chiefGui
Copy link
Collaborator

@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. 👍

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

@SomethingSexy
Copy link

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.

@platinumazure
Copy link

@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.

@SomethingSexy
Copy link

@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
Copy link

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.

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

5 participants