0
votes

I'm trying to figure out what I can use on the embedded elixir front to check if something is a number.

This is my code

<%= form_for @changeset, @action, fn f -> %>
  <%= if @changeset.action do %>
    <div class="alert alert-danger">
      <p>Oops, something went wrong! Please check the errors below.</p>
    </div>
  <% end %>
<!--- CAN I CHECK if @action is integer here? -->
  <%=if Integer.parse(@action) %>

  <div class="form-item">
    <%= label f, :shipping_address, class: "is-req" %>
    <%= text_input f, :sender_address %>
    <%= error_tag f, :label %>
  </div>

  <div class="form-item">
    <%= label f, :Receiver_Group_Name, class: "is-req" %>
    <%= text_input f, :reciever_group_name %>
    <%= error_tag f, :reciever_group_name %>
  </div>

  <div class="form-item">
    <%= label f, :Shipping_Items, class: "is-req" %>
    <%= text_input f, :items %>
    <%= error_tag f, :items %>
  </div>

  <div class="form-item">
    <%= label f, :Funding, class: "is-req" %>
    <%= text_input f, :funding %>
    <%= error_tag f, :funding %>
  </div>

  <div class="form-item is-text-center">
    <%= submit "Submit", class: "button is-big" %>
  </div>
<% end %>

So I already know Integer.parse() doesn't work on the front end, but is there anything similar I could use to check a variable? Is there a way I can interface the @action with javascript if not?

First project with elixir/Phoenix, any tips are appreciated.

1

1 Answers

2
votes

I already know Integer.parse() doesn't work on the front end”

I doubt I follow what is supposed to mean. The template is processed on the backend side. Integer.parse/2 always returns truthy values, making if Integer.parse(whatever) basically a NoOp.

What do you probably want is to setup another assign in your controller:

it_is_integer =
  case Integer.parse(action) do
    {_int, ""} -> true
    {_int, _} -> false
    :error -> false
  end

and pass this assign to Phoenix.Controller.render/3, or wherever as (it_is_integer: it_is_integer) and use in your template:

<%= if @it_is_integer %>

Sidenote: I am not familiar with Phoenix, but AFAICT @action is never supposed to be an integer.