How can I do conditional validation for OR logic, where we check to see if 1 of the 2 values is present or both values are present.
So, for example, if I want to check t
You could also create the constraint in the database, e.g. by writing a migration:
create(
constraint(
:users,
:email_or_mobile,
check: "(email IS NOT NULL) OR (mobile IS NOT NULL)"
)
)
And use check_constraint
to validate the changeset:
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:email, :first_name, :last_name, :password_hash, :role, :birthdate, :address1, :address2, :city, :state, :zip, :status, :mobile, :card, :sms_code, :status])
|> check_constraint(
:users_table,
name: :email_or_mobile,
message: dgettext("errors", "can't be blank")
)
end