LoginSignup
1
1

More than 5 years have passed since last update.

関数パターンマッチでフォーム入力前後の挙動を変える

Last updated at Posted at 2018-01-25

Phoenixでフォーム入力させる際、フォーム入力後と、フォーム未入力時や初期状態で、挙動をスイッチする場合、Elixirの関数パターンマッチが便利

foo.html.eex
<input type="text" name="basis_date" value="<%= params[ "basis_datetime" ] %>" id="datepicker"></td>

この例では、basis_dateへの日付入力(jQuery UIのDatepickerを利用)があれば、basis_date以降の更新日付をSQLのwhere句として返し、入力が無い場合や、初期状態(nil)の場合は、where句を指定しないという挙動スイッチをしている

    sql = "select * from customer #{ basis_date_where( params[ "basis_date" ] ) }"

    def basis_date_where( "" ),         do: ""
    def basis_date_where( nil ),        do: ""
    def basis_date_where( basis_date ), do: "where updated_at >= '#{ basis_date }' "
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1