AE
Ash Elixirโ€ข3y ago
axdc

Custom validation error not appearing in form

I'm using the .input and .inputs_for components from Phoenix, and I see the normal is required errors in the html when I leave fields blank, but while my custom error shows up in the changeset from IO.inspect I don't see it in the html. The validation:
defmodule Panacea.Sites.Validations.ValidateHostname do
use Ash.Resource.Validation
alias Ash.Error.Changes.InvalidAttribute

alias Ash.Changeset

@impl true
def init(opts), do: {:ok, opts}

@impl true
@spec validate(Changeset.t(), Keyword.t()) :: :ok | {:error, term()}
def validate(changeset, keyword) do
IO.inspect(changeset)
IO.inspect(keyword)

# always return an error because i am trying to see it
{:error,
InvalidAttribute.exception(
message: "hello i am an error if you cannot see me that is a bigger error"
)}
end
end
defmodule Panacea.Sites.Validations.ValidateHostname do
use Ash.Resource.Validation
alias Ash.Error.Changes.InvalidAttribute

alias Ash.Changeset

@impl true
def init(opts), do: {:ok, opts}

@impl true
@spec validate(Changeset.t(), Keyword.t()) :: :ok | {:error, term()}
def validate(changeset, keyword) do
IO.inspect(changeset)
IO.inspect(keyword)

# always return an error because i am trying to see it
{:error,
InvalidAttribute.exception(
message: "hello i am an error if you cannot see me that is a bigger error"
)}
end
end
The resource:
...
validations do
validate Panacea.Sites.Validations.ValidateHostname
end
...
...
validations do
validate Panacea.Sites.Validations.ValidateHostname
end
...
Is there something I'm missing in implementing a validation or could it be something in the form?
4 Replies
frankdugan3
frankdugan3โ€ข3y ago
I would suspect it's because this error is not tied to a specific field, so you'd have to add something to render generic form errors. Or if this is intended to be tied to a specific attribute, you need to associate it w/ the attributes that should show the error.
frankdugan3
frankdugan3โ€ข3y ago
GitHub
ash/confirm.ex at v2.6.31 ยท ash-project/ash
A declarative and extensible framework for building Elixir applications. - ash/confirm.ex at v2.6.31 ยท ash-project/ash
axdc
axdcOPโ€ข3y ago
Got it, reading through the Ash source for validations and InvalidAttribute now to try and get a sense of how it's meant to work OK! So I had noticed pretty quick that in the changeset for domains, whose error is not appearing, the field is showing up as a string whereas for my other resources whose default errors are working the field is specified as an atom. I changed the name field to be an atom and it still didn't work... then I finally changed it to gibberish and noticed it was still showing up in iex as the same field as a string. mix clean and mix compile and now everything is fine. That was the issue. I do not know why it was stuck on the old values but now everything is great ๐Ÿ™‚
defmodule Panacea.Sites.Validations.ValidateHostname do
use Ash.Resource.Validation
alias Ash.Error.Changes.InvalidAttribute

alias Ash.Changeset

@impl true
def init(opts) do
IO.inspect(opts)
{:ok, opts}
end

@impl true
@spec validate(Changeset.t(), Keyword.t()) :: :ok | {:error, term()}
def validate(changeset, opts) do
# IO.inspect(changeset)
# IO.inspect(opts)

error =
InvalidAttribute.exception(
field: :name, # <- specify field as atom and maybe mix clean just to be sure and not chase air for an hour
message: "hello i am an error if you cannot see me that is a bigger error"
)

# always return an error because i am trying to see it
{:error, error}
end
end
defmodule Panacea.Sites.Validations.ValidateHostname do
use Ash.Resource.Validation
alias Ash.Error.Changes.InvalidAttribute

alias Ash.Changeset

@impl true
def init(opts) do
IO.inspect(opts)
{:ok, opts}
end

@impl true
@spec validate(Changeset.t(), Keyword.t()) :: :ok | {:error, term()}
def validate(changeset, opts) do
# IO.inspect(changeset)
# IO.inspect(opts)

error =
InvalidAttribute.exception(
field: :name, # <- specify field as atom and maybe mix clean just to be sure and not chase air for an hour
message: "hello i am an error if you cannot see me that is a bigger error"
)

# always return an error because i am trying to see it
{:error, error}
end
end
ZachDaniel
ZachDanielโ€ข3y ago
weird that it got stuck, but glad its working now ๐Ÿ˜„

Did you find this page helpful?