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

stability of _type-field #95

Closed
lindem opened this issue Jan 6, 2016 · 6 comments
Closed

stability of _type-field #95

lindem opened this issue Jan 6, 2016 · 6 comments

Comments

@lindem
Copy link

lindem commented Jan 6, 2016

I want to check for the type of a validator for a given attribute (to determine if there is a 'presence'-validation, to adapt css classes in a form for a given field, like, 'must be filled in'). Is it safe to use the _type field of a given validator for this purpose? The underscore hints at privateness or instability.

How likely is this to change? Is there a better way to check?

@lindem
Copy link
Author

lindem commented Jan 6, 2016

It seems that #93 is related to this issue as well; maybe there is will be a common solution for both issues.

@offirgolan
Copy link
Collaborator

You are welcome to use the _type field. I dont think it will be changing anytime soon but I will try to create a solution of #93 as soon as I have some time to work on it 😄

@offirgolan
Copy link
Collaborator

#93 has been resolved and is in v2.2.0

@lindem
Copy link
Author

lindem commented Jan 14, 2016

Sorry for commenting on this again, but I have a pretty similar question once again: what about getting a validator function to validate arbitrary data (not from the model)? To call the function I can do (from a live example):

Ember.get(model, `validations._validators.${key}.${attr}`[index].validate(theData, theOptions);

I am getting theOptions from the solution in #96. The actual questions are

  • "How stable is the _validators field" and
  • "is there an easier way" (or more generically 'is this the way to do it').

The need to do this arises from form validation (and I don't want the form to operate on the actual, validation-possessing object at first, at least not on all fields).

Should I open another issue for this?

Edit. I find that I can also get a validator by using container.lookup() with the keys in the validation options object (prefixing them with validator:). Is that a better idea? Is using the container lookup stable?

@offirgolan
Copy link
Collaborator

I definitely think that using a container lookup is better than using the _validators object (thats what we do internally). I was hoping you can explain your use case a little better because Im not sure I follow

@lindem
Copy link
Author

lindem commented Jan 15, 2016

To further elaborate on my use case: We have form fields which get told which model and attribute of that model they are going to fill in, and we want these form fields to fetch the validations from the model they are configured on (either we create the record on form display or we fetch an existing one). That all works fine so far.

But. If you don't want to directly two-way-bind to a model attribute, because the user fills in a format different from the one stored (say, a date in German is dd.mm.yyyy instead of iso yyyy-mm-dd, which we want to store), you need some kind of intermediate step to translate into the format and then look at the validations from the input format and the stored format (because the stored format might have additional constraints -- we do have two validators/configs for this, then, and one is defined/mixed into the form field component, but it'd be nice to have a way to make that more flexible too -> container lookup).

In short, we want to be able to translate to the stored format and arbitrarily want to check the validity before writing to the model using exactly the configuration of the validator we defined on the model.

I admit that this might not be the best way to do this kind of thing, but then, I am slowly getting into that topic with ember and cp-val's (doing forms since early 2000s, and validation with its many edge cases has never been easy). I am open to suggestions though :-)

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

2 participants