trying to figure out a date range different from the year: [options: 1940…2019 ] trying to get a start year like 1940 to DateTime.utc_now.year - 18
this is for a registration form
Do you mean like this?
iex(2)> 1940..(DateTime.utc_now.year() - 18)
1940..2001
But even though I understand that there might be juristical reasons why you want to keep my son out of the web site, but why are you locking out my grandfather?
lol years can differ for grandpa but i tried this and it dont work <%= date_select f, :birthday, 1940…(DateTime.utc_now.year() - 18), class: “w3-select” %>
Compiling 1 file (.ex)
== Compilation error in file lib/album_web/views/user_view.ex ==
** (CompileError) lib/album_web/templates/user/form.html.eex:30: undefined function date_select/4
(stdlib) lists.erl:1338: :lists.foreach/2
(stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
(elixir) lib/kernel/parallel_compiler.ex:206: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6
You need to pass that range as year: [options: range]
.
At least thats how I understand the documentation, not using it myself.
ok is there a way to make it automatic update each year or do i have to just change it each year myself
As far as I remember eex
s rendering logic, it should be evaluated each time the form is rendered and therefore always be “correct” without further intervention.
ok thanks
So, please, I’m wondering if using DateTime.utc_now().year
like below in some Ecto schema, will actualize the current year in a form without re-compiling my project each year?
@current_year DateTime.utc_now().year
schema "projects" do
field(:ref, :string)
field(:description, :string)
field(:from_year, :integer, default: @current_year)
field(:to_year, :integer, default: @current_year)
timestamps()
end
It doesn’t. Defaults in ecto are basically the same as doing defstruct from_year: @current_year, to_year: @current_year
and therefore the values are set at compile time. If you need default values, which are dependant on runtime values (a clock is probably the most overlooked dependency in programming) you’d want to use a function to set the value.
No, you need to recompile each year, as the value of a module attribute is determined at compile time.
In general, defaults for structs have to be determined at compile time, there is no way to have a dynamic default currently.
A common practice is to use a new
function or macro which does the function call to have the dynamic default.