The field birthdate is new and its value is NULL for all rows in the database also I am trying to create a new record not updating an existing one. I can see the new value passed to the server from the browser "user" => %{"birthdate" => "2000-1-1", ...........} and the function is called in the changeset like this:
def changeset(user, attrs) do
user
|> cast(attrs, [:name, :username, :birthdate])
|> validate_required([:name, :username])
|> validate_length(:username, min: 1, max: 255)
|> validate_birthdate(:birthdate)
|> unique_constraint(:username)
end
Here is the schema (based on the phoenix book) and in this case it should be using registration_changeset:
defmodule Myapp.Accounts.User do
use Ecto.Schema
import Ecto.Changeset
alias Myapp.Accounts.Credential
require Logger
schema "user" do
field :name, :string
field :username, :string
field :birthdate, :date
has_one :credential, Credential
timestamps()
end
def changeset(user, attrs) do
user
|> cast(attrs, [:name, :username, :birthdate])
|> validate_required([:name, :username])
|> validate_length(:username, min: 1, max: 255)
|> validate_birthdate(:birthdate)
|> unique_constraint(:username)
end
def registration_changeset(user, params) do
user
|> changeset(params)
|> cast_assoc(:credential, with: &Credential.changeset/2, required: true)
end
def validate_birthdate(changeset, field) do
Logger.debug("++++")
validate_change(changeset, :birthdate, fn _, _ ->
Logger.debug("****")
[]
end)
end
end