Set error message on attribute using AshAuthentication.Strategy.Password.PasswordValidation

When a password validation fails on a liveview form submitted. How do I get an error on the :current_password attribute?
AshPhoenix.Form.submit(socket.assigns.password_form, params: params, actor: socket.assigns.current_user)
...

validate {AshAuthentication.Strategy.Password.PasswordValidation, password_argument: :current_password} do
only_when_valid?(true)
before_action?(true)
message("Password incorrect")
end
AshPhoenix.Form.submit(socket.assigns.password_form, params: params, actor: socket.assigns.current_user)
...

validate {AshAuthentication.Strategy.Password.PasswordValidation, password_argument: :current_password} do
only_when_valid?(true)
before_action?(true)
message("Password incorrect")
end
21 Replies
ZachDaniel
ZachDanielβ€’3y ago
Do you currently not get that? what error do you get currently?
Robert Graff
Robert GraffOPβ€’3y ago
if I call User.update_password(user, %{...}) with an incorrect password then I'll get the invalid error as expected but it's not associated with any fields.
{ :error,
%Ash.Error.Invalid{
...
errors: [%Ash.Error.Changes.InvalidChanges{fields: nil, message: "Password incorrect", validation: nil, changeset: nil, query: nil, error_context: [], vars: [], path: [], stacktrace: #Stacktrace<>, class: :invalid}],
...
}
{ :error,
%Ash.Error.Invalid{
...
errors: [%Ash.Error.Changes.InvalidChanges{fields: nil, message: "Password incorrect", validation: nil, changeset: nil, query: nil, error_context: [], vars: [], path: [], stacktrace: #Stacktrace<>, class: :invalid}],
...
}
And I don't see the error on form after a submit
Robert Graff
Robert GraffOPβ€’3y ago
Gist
gist:6677b4d2018c2325d2dc07557d1c2e68
GitHub Gist: instantly share code, notes, and snippets.
ZachDaniel
ZachDanielβ€’3y ago
ah that looks like a bug in AshAuthentication we should set the field on that error Would you be interested in PR-ing the fix? Should be able to track it down in the AshAuthentication codebase and add the fields: [password_field] to the exception
Robert Graff
Robert GraffOPβ€’3y ago
I'm definitely interested in that but I'm also very new to Ash, Phoenix and even Elixir. I'll make an attempt though.
ZachDaniel
ZachDanielβ€’3y ago
Just LMK if you have any questions, happy to provide some pointers πŸ˜„
ZachDaniel
ZachDanielβ€’3y ago
I think so, yeah πŸ™‚ have you tried it out?
Robert Graff
Robert GraffOPβ€’3y ago
I ran into some spark errors. I think because of different package version. I will test today @Zach Daniel confirmed it works in my app
ZachDaniel
ZachDanielβ€’3y ago
πŸ₯³ in that case πŸš€
Robert Graff
Robert GraffOPβ€’3y ago
I'm going to attempt a unit test I'm not able to run tests locally. Is there a way to trigger CICD on PRs?
ZachDaniel
ZachDanielβ€’3y ago
It should trigger automatically huh...strange that its not okay, I just added the missing config for that You may need to pull main and/or re-open your PR to make it work alright, had to approve it since its your first PR against that repo but its running now πŸ™‚
Robert Graff
Robert GraffOPβ€’3y ago
I re-opened a PR with a proper commit message
ZachDaniel
ZachDanielβ€’3y ago
yep, sorry about that πŸ€” it might be yelling about the length? Either way, you can use git commit --amend and git push --force to not have to reopen your PR Try something like this:
fix: associate password error with field

rest of description here=
fix: associate password error with field

rest of description here=
Robert Graff
Robert GraffOPβ€’3y ago
Pushed fix
ZachDaniel
ZachDanielβ€’3y ago
oops, it was still erroring but I merged it Oh well, thanks for the fix πŸ™‚
Robert Graff
Robert GraffOPβ€’3y ago
My first open source contribution in Elixir πŸŽ‰
ZachDaniel
ZachDanielβ€’3y ago
congrats! Are you on twitter? Sounds like a fun thing to shout you out for πŸ™‚ only if you'd like, of course πŸ˜„
Robert Graff
Robert GraffOPβ€’3y ago
Yep. I'm @the_rogue_bear_graff
ZachDaniel
ZachDanielβ€’3y ago
Nice! Just tweeted it. You are also the 100th contributor! πŸ₯³

Did you find this page helpful?