-
Notifications
You must be signed in to change notification settings - Fork 140
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
Multiple fields causing validation not to run properly #300
Comments
This is actually expected behavior. The validations section of the readme covers this. In particular:
We do this so your validators can know that the inputs have been type checked already. See #196 for some discussion about that. |
@tfausak let's say I have interactor which I pass to |
Unfortunately I don't think you can. It would be nice if you could get both type checking errors (like "emails is required") and validation errors (like "tag can't be blank") all at once. But in general it is not possible to know which attributes a validator validates. Consider the following example: string :x, :y
validate do
if x.downcase == y.downcase
errors.add(:x, "can't be equal to y")
errors.add(:y, "can't be equal to x")
end
end The first validator uses both |
Okay, so let's consider another example: I have a form in which I have number input. I also have corresponding integer field in my Interactor. If I leave the input blank, it will pass an empty string to the interactor causing validation to fail, as an empty string is not a number. Is there any way to mitigate this issue? What if we have an option, which converts empty strings to nils before type check? Is there any way to do it currently? |
There is! See this comment for an example: #195 (comment) I think the semantics have changes a little since then (because we introduced the class Example < ActiveInteraction::Base
integer :x,
default: nil
set_callback :type_check, :before, lambda { |interaction|
interaction.x = interaction.x.presence
}
def execute
inputs
end
end
Example.run!(x: '')
# => {:x=>nil} Edited to add: We cover callbacks in the readme: https://github.com/orgsync/active_interaction/blob/v2.1.2/README.md#callbacks |
@tfausak I figured out something like this:
Works as expected, not sure if it's better/worse than a callback though. Any way to make callback work for every input at once? |
@tfausak this works:
Still |
Are you monkey patching Either way, that's going to be less future proof than using callbacks. I would recommend putting this behavior in a concern and including it when you want this behavior. module InputMangler
extend ActiveSupport::Concern
included do
set_callback :type_check, :before, lambda { |interaction|
interaction.inputs.keys.each do |input|
interaction.public_send(
"#{input}=", interaction.public_send(input).presence)
end
}
end
end
class Example < ActiveInteraction::Base
include InputMangler
integer :x,
default: nil
def execute
inputs
end
end
Example.run!(x: '')
# => {:x=>nil} |
Looks good, I think we can close this now. Anyway, I would recommend including such concern in ActiveInteraction itself, as my problem is an issue for every Interactor used together with |
Can you create a new issue for that? I am open to discussing that addition. |
@tfausak I will try to figure something out and open PR |
Let's have an Interactor:
after passing empty string as a tag to it, I should get an error and this is exactly what happens:
Although when I have multiple fields in the interactor, as per:
Error on
tag
field is not there anymore:As a reference, here's related
Gemfile.lock
part:It's reproducible on isolated Ruby 2.2.2 instance as well.
Thanks!
The text was updated successfully, but these errors were encountered: