What I'm trying to do is passing an empty string as the value of a field, and validating it to check if not nil. Problem is that validate_required raise error on both nil and blank values. How to make it accept blank values?
schema
schema "messages" do
field :user_id, :string
field :text, :string
timestamps()
end
changeset
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:text, :user_id])
|> validate_required([:text, :user_id])
end
field :my_field, :string, default: ""
See also the docs on empty values in changesets. – Spheroidicity