Hi, please I have a resource that has two integer fields serial_start and serial_end. I want to be able to validate if one is greater than the other. Below is my code but it doesn’t validate.
defmodule Commercial.Clients.Passbook do
use Ecto.Schema
import Ecto.Changeset
alias Commerical.Clients.Passbook
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "passbooks" do
field :passbook_number, :integer
field :serial_end, :integer
field :serial_start, :integer
field :active, :boolean, default: true
belongs_to :account, Commercial.Clients.Account
belongs_to :customer, Commerical.Clients.Customer
timestamps()
end
@doc false
def changeset(%Passbook{} = passbook, attrs) do
passbook
|> cast(attrs, [:account_id, :customer_id, :passbook_number, :serial_start, :serial_end, :active])
|> validate_required([:account_id, :customer_id, :passbook_number, :serial_start, :serial_end])
|> validate_number(:serial_start, less_than: :serial_end)
|> unique_constraint(:passbook_number, message: "Passbook number already exist.")
end
end
Please how can i achieve this? Thank you.