Mark Karavan
Mark Karavan

Reputation: 2674

Custom template types in ex_admin (Phoenix)

I have a blog built in Phoenix using ex_admin on the back-end and I'd like to override some of the default inputs for my blog post template.

My post has the following properties:

  schema "posts" do
    field :title, :string
    field :image, :string   # will store as URL, will reference external host like S3
    field :created_on, :datetime
    field :content, :text

    timestamps()
  end 

I would like to do the following:

  1. Keep the :title field a text input
  2. Create an uploader input for :image
  3. Keep the multiple drop-down selection scheme that is default with Phoenix for created_on
  4. Create a Quill wysiwyg for my :content (Basically put in a script that targets the :content text field)

Ideally, I would like to do something like this:

defmodule MyBlog.ExAdmin.Post do
    use ExAdmin.Register

    register_resource MyBlog.Post do

    end

    form post do
        inputs do
            input post, :title, type: :string
            input post, :image, type: :file
            input post, :created_on, type: :datetime
            input post, :content, type: :text
        end
    end

    # Add a javascript hook that fires here to target the :content input

end

:title, :image and :created_on fields all show text inputs...the only type that I have noticed that alters the text field is type: :password. Is there a way to drop in these types dynamically from the inputs do list, or would I have to create a custom template and reference it?

Upvotes: 1

Views: 250

Answers (1)

Steve Pallen
Steve Pallen

Reputation: 4507

First, you don't need to specify the types of the fields usually. Once exception is for text fields because both text and string fields are typed as string in the schema meta data.

This should work for your javascript on the form:

form post do
   inputs do
        input post, :title, type: :string
        input post, :image, type: :file
        input post, :created_on, type: :datetime
        input post, :content, type: :text
    end
    javascript do
      form_javascript()
    end
end
def form_javascript, do: """
  $(document).ready(function() {
    // ... 
  });
"""

You really don't need to use the form_javascript function. You can just embed can just do

    javascript do
      """
      $(document).ready(function() {
         // ... 
      });
      """
    end

However, I like to separate out the javascript, especially if its fairly long.

Upvotes: 1

Related Questions