【问题标题】:Is it possible to check for an integer when using embedded elixir on the front end?在前端使用嵌入式 elixir 时是否可以检查整数?
【发布时间】:2019-04-28 14:27:00
【问题描述】:

我正在尝试弄清楚我可以在嵌入式 elixir 前端使用什么来检查某物是否是数字。

这是我的代码

<%= 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 %>

所以我已经知道 Integer.parse() 在前端不起作用,但是有什么类似的东西可以用来检查变量吗?如果没有,有没有办法可以将@action 与 javascript 接口?

elixir/Phoenix 的第一个项目,感谢任何提示。

【问题讨论】:

    标签: html elixir phoenix-framework


    【解决方案1】:

    我已经知道Integer.parse() 在前端不起作用”

    我怀疑我是否遵循了应有的意思。模板在后端处理。 Integer.parse/2 总是 返回真实值,使 if Integer.parse(whatever) 基本上是 NoOp。

    您可能想要的是在您的控制器中设置另一个 assign:

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

    并将此分配传递给Phoenix.Controller.render/3,或作为 (it_is_integer: it_is_integer) 的任何位置并在您的模板中使用:

    <%= if @it_is_integer %>
    

    旁注:我不熟悉 Phoenix,但 AFAICT @action 是 never supposed to be an integer。

    【讨论】:

    • 您对行动的看法是正确的,我只是在检查。这行得通,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 2011-01-19
    • 2020-07-11
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    相关资源
    最近更新 更多