Skip to content

Instantly share code, notes, and snippets.

@nallwhy
Last active March 5, 2024 23:50
Show Gist options
  • Save nallwhy/9460c228d77bd469b13d7740614a2882 to your computer and use it in GitHub Desktop.
Save nallwhy/9460c228d77bd469b13d7740614a2882 to your computer and use it in GitHub Desktop.
AshPhoenix Form is not valid
Application.put_env(:sample, SamplePhoenix.Endpoint,
http: [ip: {127, 0, 0, 1}, port: 5001],
server: true,
live_view: [signing_salt: "aaaaaaaa"],
secret_key_base: String.duplicate("a", 64)
)
Mix.install([
{:plug_cowboy, "~> 2.7"},
{:jason, "~> 1.0"},
{:phoenix, "~> 1.7.0"},
{:phoenix_live_view, "~> 0.20.0"},
{:ash, "~> 2.19"},
{:ash_phoenix, "~> 1.3"}
])
defmodule SamplePhoenix.ErrorView do
def render(template, _), do: Phoenix.Controller.status_message_from_template(template)
end
defmodule Author do
use Ash.Resource, data_layer: Ash.DataLayer.Ets
attributes do
uuid_primary_key :id
attribute :name, :string, allow_nil?: false
end
relationships do
belongs_to :post, Post
end
actions do
defaults [:create, :update, :destroy]
end
end
defmodule Post do
use Ash.Resource, data_layer: Ash.DataLayer.Ets
attributes do
uuid_primary_key :id
attribute :title, :string, allow_nil?: false
end
relationships do
has_one :author, Author
end
actions do
update :edit do
argument :author, :map, allow_nil?: false
change manage_relationship(:author, type: :direct_control)
end
end
end
defmodule SamplePhoenix.SampleLive do
use Phoenix.LiveView, layout: {__MODULE__, :live}
def mount(_params, _session, socket) do
post = %Post{
id: Ash.UUID.generate(),
title: "title",
author: %Author{id: Ash.UUID.generate(), name: "name"}
}
form =
post
|> AshPhoenix.Form.for_update(:edit, forms: [auto?: true])
|> to_form()
socket =
socket
|> assign(form: form)
{:ok, socket}
end
# layout
def render("live.html", assigns) do
~H"""
<script src="https://cdn.jsdelivr.net/npm/phoenix@1.7.0-rc.2/priv/static/phoenix.min.js">
</script>
<script
src="https://cdn.jsdelivr.net/npm/phoenix_live_view@0.18.2/priv/static/phoenix_live_view.min.js"
>
</script>
<script>
let liveSocket = new window.LiveView.LiveSocket("/live", window.Phoenix.Socket)
liveSocket.connect()
</script>
<style>
* { font-size: 1.1em; }
</style>
<%= @inner_content %>
"""
end
# render
def render(assigns) do
~H"""
<.form id="form" for={@form} phx-change="validate" phx-submit="save">
<input type="text" id={@form[:title].id} name={@form[:title].name} value={@form[:title].value} />
<.inputs_for :let={fa} field={@form[:author]}>
<input type="text" id={fa[:name].id} name={fa[:name].name} value={fa[:name].value} />
</.inputs_for>
<button type="submit" disabled={!@form.source.valid?}>Save</button>
</.form>
<%= inspect(@form) %>
"""
end
def handle_event("validate", %{"form" => params}, socket) do
form = socket.assigns.form |> AshPhoenix.Form.validate(params)
socket =
socket
|> assign(form: form)
{:noreply, socket}
end
def handle_event("save", _params, socket) do
{:noreply, socket}
end
end
defmodule Router do
use Phoenix.Router
import Phoenix.LiveView.Router
pipeline :browser do
plug(:accepts, ["html"])
end
scope "/", SamplePhoenix do
pipe_through(:browser)
live("/", SampleLive, :index)
end
end
defmodule SamplePhoenix.Endpoint do
use Phoenix.Endpoint, otp_app: :sample
socket("/live", Phoenix.LiveView.Socket)
plug(Router)
end
{:ok, _} = Supervisor.start_link([SamplePhoenix.Endpoint], strategy: :one_for_one)
Process.sleep(:infinity)
@rapidfsub
Copy link

rapidfsub commented Mar 5, 2024

-   def handle_event("validate", params, socket) do
-     form = socket.assigns.form |> AshPhoenix.Form.validate(params)
+   def handle_event("validate", %{"form" => form}, socket) do
+    form = socket.assigns.form |> AshPhoenix.Form.validate(form)

params만 바꿔주심 됩니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment