I have an Ecto schema with a field that I want to make required but potentially calculated from another field if it’s nil
. Here’s a simplified version:
schema "users" do
field :email, :string, null: false
field :username, :string, null: false
end
I want to enable specifying a username
but for the username to default to the value of the email
field if no username is specified. My changeset function is currently looking like this:
def changeset(changeset, attrs \\ %{}) do
changeset
|> cast(attrs, [:email, :username])
|> validate_required([:email, :username])
|> set_username()
end
def set_username(changeset) do
case get_field(changeset, :username) do
nil ->
put_change(changeset, :username, changeset.email)
_username ->
changeset
end
end
But this is throwing errors that I’m violating the non-null constraint for username
. I’ve also tried putting the set_username
function before the validate_required
and cast
functions but that hasn’t worked either. Any ideas?