AshPhoenix potential bug with relationships

I have the following form:
<.simple_form
:let={f}
for={@form}
id="add-flight-form"
phx-submit="submit"
phx-change="validate"
>
<.input
field={{f, :plane}}
type="select"
label="Airplane"
options={Enum.map(@fleet, & &1.registration)}
/>
<.input field={{f, :number}} label="Number" />
<.input field={{f, :departure}} label="Departure" />
<.input field={{f, :arrival}} label="Arrival" />
<.input field={{f, :alternate}} label="Alternate" />
<:actions>
<.button>Save</.button>
</:actions>
</.simple_form>
<.simple_form
:let={f}
for={@form}
id="add-flight-form"
phx-submit="submit"
phx-change="validate"
>
<.input
field={{f, :plane}}
type="select"
label="Airplane"
options={Enum.map(@fleet, & &1.registration)}
/>
<.input field={{f, :number}} label="Number" />
<.input field={{f, :departure}} label="Departure" />
<.input field={{f, :arrival}} label="Arrival" />
<.input field={{f, :alternate}} label="Alternate" />
<:actions>
<.button>Save</.button>
</:actions>
</.simple_form>
I do get an error if the number or plane field is missing, but I don't have any errors for the departure, arrival and alternate fields. All 3 of these fields have a similar relationship on my resource that looks like this:
belongs_to :departure_airport, Vigil.Airports.Airport do
api Vigil.Airports
attribute_writable? true
allow_nil? false
source_attribute :departure
destination_attribute :icao_code
attribute_type :string
end
belongs_to :departure_airport, Vigil.Airports.Airport do
api Vigil.Airports
attribute_writable? true
allow_nil? false
source_attribute :departure
destination_attribute :icao_code
attribute_type :string
end
If I inspect the form, I don't see any errors at all regarding those fields, as if they weren't attributes at all, but I do see the attributes data: attributes: %{alternate: nil, arrival: nil, departure: nil, number: nil}, Shouldn't the form show errors about those fields being required?
6 Replies
ZachDaniel
ZachDaniel3y ago
Ah, interesting. So the issue is that for belongs_to relationships we delay their validation until just before the action is submitted. This is because manage_relationship might set the value after the action starts. But in retrospect, this doesn't really make sense. What we should do instead is only do that for things that have a manage_relationship set up for them I will add this to my list to fix.
richaard0
richaard0OP3y ago
Nice thanks for the quick reply! 🙂
ZachDaniel
ZachDaniel3y ago
Okay, I've pushed some stuff to main that should resolve this. I'm still poking around in some of my apps and making sure there aren't weird downstream consequences. If it proves to be troublesome than I will probably need to save it for a major release (its possible people were relying on this in a weird way).
richaard0
richaard0OP3y ago
Thank you Zach! However I pulled main and I don't see anything different 😦 Is it on AshPhoenix's main branch or Ash itself?
ZachDaniel
ZachDaniel3y ago
🤔 interesting Its ash's main branch is that what you pulled?
richaard0
richaard0OP3y ago
Nah I pulled AshPhoenix, that explains it 😄 Works perfectly 😁

Did you find this page helpful?