anagrius
Hi,
I have a a complex set of policies and validations in my resource. I would like to enable or disable a button depending on if:
A. The record is validate
B. The policies allow the actor to perform the action.
When I was initially starting out I had implemented much of the validation logic as policies.
Now I have migrated many of the checks to validation, as that is what they really are. Violations would bring the data into an illegal state.
The issue is that now my:
<button disabled={
Resource.can_perform_action?(@user, @resource)} >...</button>
Always returns true as the actor is allowed even if the resource is not valid.
I assume there is a function for a dry-run validation of the action, but I cannot find it.
<button disabled={
Resource.valid_for_action?(@user, @resource)
&& Resource.can_perform_action?(@user, @resource)} >...</button>
Something like that. Am I off on the wrong path, should the can_perform_action? also be performing validations?
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
olivermt
No, what you want here is a calculated property that tells you if this is allowed or not I think.
This calculation should/could probably share code with your policies to not repeat yourself.
This was discussed on slack or discord the other day, but I can’t find it now.
anagrius
Hm, maybe you could give an example of how that might look?
There are like 10-20 validations in some actions that feed into if an action being valid or not.
zachdaniel
The way to run validations would be to build a changeset or query for the action and check
query_or_changeset.valid?However, what you’re looking to do can make sense as a new option to
can. I’ve pushed support for this to ashmainif you’d like to try it out.can_perform_action?(@user, @resource, validate?: true)olivermt
I absolutely misread your code (and misunderstood your intention). Apologies
anagrius
Thanks Zach, that is exactly what I am looking for. I will try it out and report back.
anagrius
So, I had a change to test out
:validate?on the main branch, and it almost does what I would want except it actually raises an error in case of validation errors.I would probably expect it to just answer
false, and in casesreturn_errors?is set i get the list so I can display to they user why it cannot perform an action.It might an idea to slight change
can_*functions to be either validation, authorization or both. Something likechecks:[:validation, :authorization], and areturn_errors?` flag.I could see use cases for seeing if an action COULD be done validly if the user had permission, or if the action was permitted.
On the other hand it might just as well be two functions
authorized?()andvalid?()at that point maybe.zachdaniel
Ah, right that isn’t what I was going for. Can you update to latest main? I’ve made it treat invalid as a false result.
anagrius
Works like a charm now
It would be very cool to be able to display “why” an button is disabled for validations as well just having the disabled button is not nice UX. I don’t know if
can_*should be the functions for that though… In any case this is already much nicer to use.zachdaniel
If you use
caninstead ofcan?you can get the error back, which will beForbiddenfor forbidden errors and invalid for changeset errors.onetwothree
Hey, I was looking for some docs regarding it and I can find nothing if I search for
can_perform_actionin ash organisation, could anyone point me to proper place?