Ash FrameworkAF
Ash Framework3y ago
8 replies
Edward Scott

How to determine the cause of a form validation error

I have a couple of resources:
`
defmodule XrtConfiguratorApp.Template.Component do
  # Using Ash.Resource turns this module into an Ash resource.
  use Ash.Resource,
    # Tells Ash you want this resource to store its data in Postgres.
    data_layer: AshPostgres.DataLayer

  # The Postgres keyword is specific to the AshPostgres module.
  postgres do
   ...
  end

  # Defines convenience methods for
  # interacting with the resource programmatically.
  code_interface do
    define_for XrtConfiguratorApp.Template
    define :create, action: :create
    define :read_all, action: :read
    define :update, action: :update
    define :destroy, action: :destroy
    define :get_by_id, args: [:id], action: :by_id
  end

  actions do
    # Exposes default built in actions to manage the resource
    defaults [:create, :read, :destroy]

    update :update do
      argument :variables, {:array, :uuid}
      change manage_relationship(:variables, type: :direct_control)
    end

    # Defines custom read action which fetches post by id.
    read :by_id do
 ...
  end

  # Attributes are simple pieces of data that exist in your resource
  attributes do
    # Add an autogenerated UUID primary key called `:id`.
    uuid_primary_key :id
    # Add a string type attribute called `:name`
    attribute :name, :string do
      # We don't want the name to ever be `nil`
      allow_nil? false
    end

    attribute :description, :string

    # Add a string type attribute called `:content`
    # If allow_nil? is not specified, then content can be nil
    attribute :content, :string do
      allow_nil? false
    end

    attribute :optional, :boolean do
      allow_nil? false
    end

    attribute :device_service, :boolean do
      allow_nil? false
    end
  end

  identities do
    identity :name, [:name]
  end

  relationships do
    has_many :variables, XrtConfiguratorApp.Template.Variable
  end

end
Was this page helpful?