jononomo

jononomo

I have a form in my EEX file in my Phoenix project as follows:

<%= form_for @conn, Routes.user_path(@conn, :create), [as: :user], fn f -> %>

    <div>
      <%= label f, :email, class: 'sr-only' %>
      <%= email_input f, :email, required: true %>
    </div>

    <div>
      <%= label f, :password, class: 'sr-only' %>
      <%= password_input f, :password, required: true %>
    </div>

    <%= submit "Log in" %>

<% end %>

This generates a form with a button as follows:

<button type="submit">Log in</button>

But I would like more control over what is inside the <button></button> tag – I would like to add a bunch of other HTML inside the button tag, including some SVG images.

<button type="submit">
    <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6V4m0 2a2 2 0 100 4m0-4a2 2 0 110 4m-6 8a2 2 0 100-4m0 4a2 2 0 110-4m0 4v2m0-6V4m6 6v10m6-2a2 2 0 100-4m0 4a2 2 0 110-4m0 4v2m0-6V4"/>
    </svg>
</button>

How can I add my own SVG tags inside the button tag while still using the Phoenix.HTML.Form module as a helper to generate the button.

Or does it just not make sense for me to use the <%= submit "Log in" %> shortcut for my case? Since I’m doing everything within the form_for function, I’d like to be consistent with the way I generate other form elements, and also make sure I have my form all tied together properly.

Showing Posts 1 to 8

LostKobrakai

LostKobrakai

You can use do/end blocks instead of string labels for many html functions:

c4710n

c4710n

As a supplyment, it’s better to manage svg image with some helpers. Or, it will interfere with your view.

jononomo

jononomo OP

When I try this code:

<%= submit "Log in" do %>
    <svg>
        ETC
    </svg>
<% end %>

I get the error undefined function submit/3

I guess that adding the do...end is like adding a third argument. So I guess submit/2 exists but submit/3 does not?

LostKobrakai

LostKobrakai

Skip the "Log in". It can be either text or a do/end block.

jononomo

jononomo OP

Currently I pass information to the submit form helper so that I can style the button by setting the class.

<%= submit "Log in", class: "styling" %>

How can I set the class on the button if I jump directly into the do-end block?

Also, under what scenario do I gain a big advantage from using this submit helper from Phoenix.HTML.Form? It seems to me like it doesn’t really help much – it seems like different syntax for saying the same thing and I’m getting no magic lifting happening for me behind the scenes.

l00ker

l00ker

The button text (or other content) just gets placed inside the do-end block:

<%= submit class: "styling" do %>
  "Log In"
<% end %>

In this case "Log In" could be replaced with your <svg />.

Have a look at Phoenix.HTML.Tag.content_tag/2/3 to get a better understanding of how this works.

jononomo

jononomo OP

Thanks!!

crockwave

crockwave

In a Phoenix Liveview leex file, I reference an SVG in the button tag, then use CSS to style the button shape/color and the SVG attributes, such as position. Also, the button is disabled if the associated input field value is blank.

                  <button class="post_send_button" type="submit" name="submit" <%= if @post == "" do %>disabled="disabled"<% end %>>
                    <img class="send_img" src="<%= Routes.static_path(@socket, "/images/icons/send_white.svg") %>"/>
                  </button>

Following is CSS that renders a round button with the SVG centered, and to the right side of the associated input assigned a class value of post_send:

.post_send {
  display: flex;
  padding-bottom: 1.0rem;
}

.post_send_button {
  border-radius: 16px;
  height: 32px !important;
  width: 32px;
  margin-left: 0.3rem;
  margin-bottom: 0px;
  padding: 0px;
}

.send_img {
  height: 18px;
  width: 21px;
  margin: 0px;
  line-height: 0px !important;
  position: relative;
  left: -1px;
  top: 2px;
}
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews