This code is intended to validate a user typed DELETE before deleting their account, but it doesn’t work because add_error
isn’t adding the error nor making the changeset valid?: false
. Perhaps the issue might be that it’s run inside before_action
which was needed so that the validation doesn’t happen until form submission. What’s the right way to accomplish this? Thanks.
change fn changeset, _ ->
Ash.Changeset.before_action(changeset, fn changeset ->
if Ash.Changeset.get_argument(changeset, :confirm_word) == "DELETE" do
changeset
else
changeset
|> Ash.Changeset.add_error(
field: :confirm_word,
message: "Type DELETE to confirm"
)
end
end)
end
This looks right at first glance. You’re getting into that code but the error is somehow not being added? What if you do
changeset
|> Ash.Changeset.add_error(
Ash.Error.to_ash_error(field: :confirm_word,
message: "Type DELETE to confirm"
))
I don’t think it will help but can’t hurt to try. You can confirm this code is being called but it’s not adding an error? Like changeset.errors
is still an empty list after you do that?
Adding Ash.Error.to_ash_error didn’t make any difference. The error list is still blank in the changeset. BTW, my changeset before_action code block is located inside a (soft) destroy action. When I try the same code in an update action, it does add these errors to the changeset.
with to_ash_error
errors: [
%Ash.Error.Unknown.UnknownError{
error: "Type DELETE to confirm",
field: nil,
changeset: nil,
query: nil,
error_context: [],
vars: [],
path: [],
stacktrace: #Stacktrace<>,
class: :unknown
}
],
without to_ash_error - InvalidAttribute because confirm_word
is not an attribute of User resource.
errors: [
%Ash.Error.Changes.InvalidAttribute{
field: :confirm_word,
message: "Type DELETE to confirm",
private_vars: nil,
value: nil,
changeset: nil,
query: nil,
error_context: [],
vars: [],
path: [],
stacktrace: #Stacktrace<>,
class: :invalid
}
],
Very interesting. I think I may know the issue. I’ll have to reproduce it and confirm when I’m home later this evening. If you can open a GH issue on ash core with all of this detail, I should have the issue resolved tonight or tomorrow. Does that work for you?
1 Like
Interesting, I’m actually having trouble reproducing this. Are you on 2.x or 3.x?
Is the example you’re showing a simplified example? Are you sure you’re properly returning the changeset with the error, that kind of thing? I’ve tested it against multiple data layers/resources and it appears to work as expected.
No, it’s not a simplified example. Let me try to create a minimal example that reproduces the issue.
1 Like
Ash 2.21.6
Ash_Phoenix 1.3.4