vic / params

Easy parameters validation/casting with Ecto.Schema, akin to Rails' strong parameters.
http://hex.pm/packages/params
Apache License 2.0
361 stars 46 forks source link

Default value for time/dates? #19

Open brandonparsons opened 7 years ago

brandonparsons commented 7 years ago

Is there any way to specify a default value for a field of type time? e..g Set ~T[16:30:00] as a default value?

I tried:

[field: :time, default: ~T[16:30:00]]

But it throws an error: ** (CompileError) nofile: invalid quoted expression: ~T[16:30:00]

vasspilka commented 4 years ago

It should be possible to use the full syntax of the time in the default (below), the error was due to macro compilation issues.

  %Time{
  calendar: Calendar.ISO,
  hour: 16,
  microsecond: {0, 0},
  minute: 30,
  second: 0
}
dimakula commented 3 years ago

I had a similar issue, I tried using %Time{} and Time.new/3 directly but it came back with the same error. The error message may have improved since this was reported, because it also contains the solution:

Please make sure your quoted expressions are made of valid AST nodes. If you would like to introduce a value into the AST, such as a four-element tuple or a map, make sure to call Macro.escape/1 before

Just wrap the time inside of a Macro.escape/1 call and it will work:

[field: :time, default: Macro.escape(~T[16:30:00])]