wdtnh

wdtnh

I have a phoenix app with a lot of related tables. In particular, I have a regions (think territory) as a parent table that has a related child table consisting of 3-digit zip codes that are in a table region_zips.
I need a select form element in the region_zip form (created by phx.gen.html) in order to make sure I’m picking the correct region for a particular 3 digit zip code. I cannot get this to work no matter how many ways I’ve tried. I know the lookup in def new(conn, _params) works. It’s just not working in the form. I would greatly appreciate any assistance. Thanks in advance. Bill

def new(conn, _params) do
    regions = Regions.lookup_regions() #  Valid results are [%{id: 1, name: "Test" },...]
    changeset = RegionZips.change_region_zip(%RegionZip{})
    render(conn, :new, changeset: changeset, regions: regions)
  end

The related form is

<.simple_form :let={f} for={@changeset} action={@action}>
  <.error :if={@changeset.action}>
    Oops, something went wrong! Please check the errors below.
  </.error>
  <.input field={f[:region_id]} type="select" options={@regions} label="Region" />
  <.input field={f[:zip_prefix]} type="text" label="Zip prefix" />
  <:actions>
    <.button>Save Region zip</.button>
  </:actions>
</.simple_form>

Showing Posts 1 to 4

wdtnh

wdtnh OP

Appreciate the reply but that wasn’t it. I’ve formatted the regions lookup in a number of ways as an array of maps, tuples, keywords, i, etc. I still get key error message that lookup of regions passed as an arg to the render function executes.

KeyError at GET /region_zips/new
key :regions not found in: %{
  action: "/region_zips",
  __given__: %{
    action: "/region_zips",
    __changed__: nil,
    changeset: #Ecto.Changeset<
      action: nil,
      changes: %{},
      errors: [
        region_id: {"can't be blank", [validation: :required]},
        zip_prefix: {"can't be blank", [validation: :required]}
      ],
      data: #Crm.RegionZips.RegionZip<>,
      valid?: false,
      ...
    >
  }, 
jdiago

jdiago

Take a look at the implementation of category_opts/1 in this guide. It’s the same as what you’re trying to do.

edit: also, the reason why @regions isn’t found in the form assigns is because it has to be passed in when the form is rendered in new.html.heex. Your @regions assign exists for the new action not the form, which is a function component. There should be a line like this where you’ll have to add regions as an attr for the form.

<.thing_form regions={@regions} changeset={@changeset} action={~p"/things"} />
wdtnh

wdtnh OP

Thanks. I’ll check it out and reply back when I get a chance to work on it.

wdtnh

wdtnh OP

I’ve been traveling & haven’t had time to dig into this but I believe you hit the nail on the head of what my problem was. I absolutely love Elixir but sometimes I get lost how things get passed along. In the related region_zips VIEW field, I will pass regions as an attribute which I failed to do. I see where I was previously passing in as an action. I’ve already corrected for the proper formatting of the options in terms of key and value pairs. Thanks for the assist it is greatly appreciated.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews