Don't show existing value of form field
I have a sensitive value that I don't want displayed again, but I want an input to be able to update the value. Is there an option that exists for that?
31 Replies
Hmm…there are definitely ways. You can track the value yourself in an assign
And then do
value={@assign}
And then on change you can assign to that assignso I found this
exclude_fields_if_empty
now I just need to have the current values not selected
oh maybe just need to modify the Api.get
so I'm trying to override the action to deselect fields but I don't know if I'm setting this up right the action mmm it's simpler than this isnt it
action has takes an argument
argument updates the attribute
just don't show the secret field in a form
Oh ya that makes sense, thanks!
ok I got it kinda of working, only problem is I can't quite figure out how to make it not override the old value if the value is empty
I tried
set_when_nil?: false
but that never lets it get set🤔 There are options you can pass into the form when you create it to modify the paramus
might want to check the docs to see those options
but you could also update the params before sending them in.
I was looking in the docs and not seeing anything, I also tried
exclude_fields_if_empty
but I don't understand the behavior as it didn't work as I thought it would
so even removing the empty values from params still didn't work, because I have the action always setting it, the empty value still replaces the old value
is there a way to tell an action to only change if a value is passed?Oh are you using
change set_attribute
?
change set_attribute(:foo, arg(:foo), set_when_nil?: false)
ya, the
set_when_nil?: false
is making it so the field never gets set even the value is present, not sure what is going on there
this is what I finally got working
then I pass that to submit🤔
I bet
set_when_nil
isn't handling the empty string
so we probably need to make set_when_nil
cast the value as input to see if it results in a nil
value
I'd suggest doing something like:
And then you can do delete_if_empty("cacertfile", ...)
didn't realize you have new_cacertfile
so maybe not
🤷♂️the delete_if_empty might be good actually, because now I can't set values to empty now hahaha, with that function I can delete specific keys
I just have a custom changeset (as I have to encrypt my field)
I also encrypt my field, but that happens in a type
But got it all working, appreciate the help
I've actually had to change my type-based value encryption
to have it done as a change on the resource
because Ecto internally was double-encrypting the same value
this is how I'm doing it, is ecto double encrypting this?
Yes. Basically if you write the same value back to the encrypted type it won’t know not to encrypt it again
Hmm I've been using this for a while and have not had any issues
I don’t recall the specific issue, I’d just keep an eye out for it basically.
oh you weren't saying it will cause an issue, just doing double work
No, it caused an issue
Because decrypting it no longer yielded the original value
hmm definitely haven't run into that
So for safety’s sake I made the unencrypted values into arguments and made a change that encrypts and writes the arguments when set.
🤷♂️ not sure what’s different about mine and your app/implementation.
is there a way to make an argument only run the change if set? I was running into that issue of it always setting the value to null if not passed
But if it works for you then no worries.
GitHub
ash_hq/lib/ash_hq/changes/encrypt.ex at main · ash-project/ash_hq
The Ash Framework homepage and documentation site. - ash_hq/lib/ash_hq/changes/encrypt.ex at main · ash-project/ash_hq
So actually that just checks if a given attribute is being changed, and if so rewrites the change to the encrypted value
Not using arguments, I lied 🙂
ah custom change
Yeah
maybe we need to add a change_if_set builtin
or maybe that is what set_if_nil is for, but for some reason that was just never setting that value for me even when passed with a non nil value
Yeah I think that one is for empty strings in forms
Is basically it’s own issue
We’d need to cast the value to see if it would cast to
nil
An example of a place we might cause double encryption with your type TBHset_when_nil?
is unfortunately not very clearly named, and actually specifies the behaviour based on the existing value as opposed to the new value of the changeset.Oh, right you are 🤦♂️