kodi

kodi

Only the title, location, and description fields are showing in the form. The option_date field is not showing in the browser.

Poll schema:

defmodule Rallly6.PollingSystem.Poll do
use Ecto.Schema
import Ecto.Changeset

alias Rallly6.PollingSystem.Option

@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema “polls” do
field :description, :string
field :title, :string
field :location, :string
field :time_zone, :naive_datetime
field :is_closed, :boolean, default: false
field :is_deleted, :boolean, default: false
field :disable_comments, :boolean, default: false
field :user_id, :binary_id
has_many :options, Option

timestamps(type: :utc_datetime)

end

@doc false
def changeset(poll, attrs) do
poll
|> cast(attrs, [:title, :description, :location, :time_zone, :is_closed, :is_deleted, :disable_comments])
|> cast_assoc(:options, with: &Option.changeset/2)
|> validate_required([:title, :description, :location, :time_zone, :is_closed, :is_deleted, :disable_comments])
end
end

Option schema:

defmodule Rallly6.PollingSystem.Option do
use Ecto.Schema
import Ecto.Changeset

alias Rallly6.PollingSystem.Poll

@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema “options” do
field :option_date, :date

belongs_to :poll, Poll


timestamps(type: :utc_datetime)

end

@doc false
def changeset(option, attrs) do
option
|> cast(attrs, [:option_date, :poll_id])
|> validate_required([:option_date])
end
end

form component:

<.header> <%!-- <%= @title %> --%> <:subtitle>Use this form to manage poll records in your database.
  <.simple_form
    for={@form}
    id="poll-form"
    phx-target={@myself}
    phx-change="validate"
    phx-submit="save"
  >


  <.input field={@form[:title]} type="text" label="Title" />
  <.input field={@form[:location]} type="text" label="Location" />
  <.input field={@form[:description]} type="text" label="Description" />

  <.inputs_for :let={opt_nested} field={@form[:options]}>


    <.input field={opt_nested[:option_date]} type="date" label="date" />

  </.inputs_for>


    <:actions>
      <.button phx-disable-with="Saving...">Save Poll</.button>
    </:actions>
  </.simple_form>
</div>

In the above form component, the:option_date is not showing in the browser. I need help in fixing it

First Post!

03juan

03juan

Hello and welcome to the community.

How are you creating the @form assigns in your controller?

Last Post!

kodi

kodi

Like this and this form component auto-generated and modifying based on my need.

defmodule Rallly6Web.CreatePollLive.FormComponent do
use Rallly6Web, :live_component

alias Rallly6.PollingSystem

@impl true
def render(assigns) do
~H"“”


<.header>
<%!-- <%= @title %> --%>
<:subtitle>Use this form to manage poll records in your database.</:subtitle>
</.header>

  <.simple_form
    for={@form}
    id="poll-form"
    phx-target={@myself}
    phx-change="validate"
    phx-submit="save"
  >


  <.input field={@form[:title]} type="text" label="Title" />
  <.input field={@form[:location]} type="text" label="Location" />
  <.input field={@form[:description]} type="text" label="Description" />

  <.inputs_for :let={opt_nested} field={@form[:options]}>


    <.input field={opt_nested[:option_date]} type="date" label="date" />

  </.inputs_for>


    <:actions>
      <.button phx-disable-with="Saving...">Save Poll</.button>
    </:actions>
  </.simple_form>
</div>
"""

end

@impl true
def update(assigns, socket) do

{:ok,
 socket
 |> assign(assigns)
 |> assign_form(assigns.poll_changeset)}

end

@impl true
def handle_event(“validate”, poll_params, socket) do
changeset =
socket.assigns.poll
|> PollingSystem.change_poll(poll_params)
|> Map.put(:action, :validate)

{:noreply, socket}

end

defp assign_form(socket, %Ecto.Changeset{} = changeset) do
assign(socket, :form, to_form(changeset))
end

end

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement