-
-
Notifications
You must be signed in to change notification settings - Fork 141
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
Conditional validations and fields #66
Comments
Hmm unless I'm misreading it, I think you could do this using ember-changeset-validations with a custom validator. Are you using that add-on for validations? This add-on is only the logic for the changeset itself. |
Hi @poteto ! I am indeed using Upon thinking over this further, I don't think it's just disabling the validation that is needed, because in that case you might just return true and assume the attr in valid. What is really needed is excluding that value from the |
@ivanvanderbyl The validator function receives the https://github.com/DockYard/ember-changeset-validations#writing-your-own-validators |
Yes, I noticed that, however Imagine the scenario: You have a form with a conditional section, in this case it collects a bunch of EC2 configuration details like VPC ID, Security Group, AZ, and Subnets. These are all required fields only if the "Use EC2" checkbox is ticked, otherwise they must send the default value of that attr on the model, which is Now I can easily discard the validations of these properties if A solution I'm attempting is adding an additional form helper to |
I see, my original intention for not passing in the I suspect your use-case would be better off handled by a custom action rather than in a validator. Let me know if you manage to solve this with the action / merging, or if not we can revisit / discuss again! |
I like that approach, oh and by validator I was actually referring to a custom validate action, not the validator function from This approach so far has one snag: you can't merge invalid changesets, which is probably something you want, but now I'm trying to think off a good way to keep track of multiple changesets, and aggregate their errors so that a single list of errors is available to the form. Shouldn't be impossible :) |
I think I wouldn't mind merging invalid changesets, I was probably a little heavy handed there. Want to open a PR for it? |
You know, that would actually make this a lot easier. I'll create a PR in the morning :) |
I know I know this is very old, but I'm facing the same issue, and I don't quite understand how I ask because I'm also implementing a conditional validation. I'm able to get this to work mostly, but the changeset doesn't seem to re-validate correctly because the conditional validation depends on attrs other than the one being updated. This leads me to believe I might be missing a possible use for |
@allthesignals Do you have an example (or dummy app)? I'd be happy to look into this and possibly get working. |
@snewcomer I think I got it working, I have a PR up, here's a component class in particular: https://github.com/NYCPlanning/labs-applicant-portal/pull/207/files#diff-fa73b7a98e0df38cb1686492b3077971 Here's my conditional validator: https://github.com/NYCPlanning/labs-applicant-portal/pull/207/files#diff-2a9c92ebab030a35b86d85c9f750a967 in the same PR. There is an issue with conditional validators that I was able to get around by explicitly re-validating a changeset when a field input changed. I was mainly asking because I've gone through multiple iterations of this, trying to land on the "right" approach. The approach I linked above seems simplest (before I was wrapping stuff in proxies). |
I have a use case where some form fields are only required if a certain value is selected somewhere else in the form (in this case a checkbox). Upon investigating how validations and changesets work, I can see it's not so trivial to add support for this because not only do you need to not disable the validations, but you also need to ensure that field doesn't get propagated to the model on execute.
Any thoughts on this? Am I missing some not so obvious way to achieve this? Otherwise, I'll have a crack at implementing it soon.
The text was updated successfully, but these errors were encountered: