Ecto validate_length on null field

Hi everyone,

I have question with Ecto. Why we couldn’t have validate_length when a field which is not present?

Ex:

defmodule MyApp.Test do

schema "test" do
    field(:foo, :string)
    field(:bar, :string)
    timestamps()
  end

 def build(params) do
  %Test{}
    |> cast(params, [:foo, :bar])
    |> validate_required([:foo])
    |> validate_length(:bar , max: 10)
end
end

And if we try to have MyApp.Test.build(%{foo: "foo"}) get the error like:
** (ArgumentError) unknown field :bar in %MyApp.Tes{__meta__: #Ecto.Schema.Metadata<:built,

Can you show your actual code? validate_length does work on fields that are not passed in, I bet you have a typo in your actual code.

2 Likes

This looks like a typo :slight_smile:

1 Like
Summary