Lookup value from relation from condition callback #446
-
For a condition callback of a field I need to get the value of a relation of another field. In code I need to do something like that:
The problem is, that payload is not initialized in the callback. req.payload is not available in this callback. Is it possible to get a valid pointer for it? Do I have to get the value by doing a fetch() to localhost? Thank you very much in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @Salomo — I can give you some pointers here! So, Instead, you should just do a One note though—field conditions are run quite frequently, because every time a field value changes, you need to run the condition. For a use case like this, I'd probably build a custom component, where you can make that fetch much less frequently. That would be more performant. What kind of field needs this type of condition? As of import { TextInput, SelectInput } from 'payload/components/forms' Then you would just "wrap" the built-in Payload component with your own logic, showing and hiding the field accordingly. We will be exposing more and more built-in field types so if you need a field type that's not currently exposed, let us know and we will add it. 👍 |
Beta Was this translation helpful? Give feedback.
Hey @Salomo — I can give you some pointers here!
So,
payload.findByID
is a local API method, which means it's only available in contexts where you're executing code on your server. As conditions are executed in the browser, those local API methods are not available.Instead, you should just do a
fetch
. Which, if you think about it, because you're in the browser—that's really the only way to do something like that.One note though—field conditions are run quite frequently, because every time a field value changes, you need to run the condition. For a use case like this, I'd probably build a custom component, where you can make that fetch much less frequently. That would be more performant.
W…