4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Mojoliciousでfillinformする

Posted at

拙作、Mojolicious::Plugin::FillInFormLite を使うと行えるようになる。

cpanmでインストール。

$ cpanm Mojolicious::Plugin::FillInFormLite

Plugin読み込み設定。

myapp.pl
use Mojolicious::Lite;

plugin 'FillInFormLite';

Controllerに新しくrender_fillinformというメソッドが追加される。

以下のように使用する。

myapp.pl
#!/usr/bin/env perl
use Mojolicious::Lite;

plugin 'FillInFormLite';

get '/user/update' => sub {
    my $self = shift;

    my %filled = (
        name => '太郎',
        gender => 'male',
        birthday_year => 1983,
        birthday_mon  => 12,
        birthday_day  => 27,
        lang => ['perl','php','ruby'],
        note => "ほげほげほげほげ\nほげほげほげ",
    );

    $self->render_fillinform(\%filled, mons => [1..12], days => [1..31]);
} => 'user/update';


app->start;
__DATA__

@@ user/update.html.ep
<html>
<head>
<meta charset="utf8">
<title>ユーザー情報</title>
</head>
<body>
<h1>ユーザー情報</h1>
<form action="<%= url_for %>" method="post">
<table>
<tr>
  <td>お名前</td>
  <td><input type="text" name="name"></td>
</tr>
<tr>
  <td>性別</td>
  <td>
    <input type="radio" name="gender" value="male">男
    <input type="radio" name="gender" value="famale">女
  </td>
</tr>
<tr>
  <td>誕生日</td>
  <td>
    <input type="text" name="birthday_year">年
    <select name="birthday_mon">
    <% for my $mon (@$mons) { %>
    <option name="birthday_mon"><%= $mon %></option>
    <% } %>
    </select>月
    <select name="birthday_day">
    <% for my $day (@$days) { %>
    <option name="birthday_day"><%= $day %></option>
    <% } %>
    </select>日
  </td>
</tr>
<tr>
  <td>使用言語</td>
  <td>
    <input type="checkbox" name="lang" value="perl">Perl
    <input type="checkbox" name="lang" value="php">PHP
    <input type="checkbox" name="lang" value="ruby">Ruby
    <input type="checkbox" name="lang" value="python">Python
    <input type="checkbox" name="lang" value="go">Go
  </td>
</tr>
<tr>
  <td>備考</td>
  <td><textarea name="note" rows="10"></textarea></td>
</tr>
</table>
<input type="submit" value="更新">
</form>
</body>
</html>

スクリーンショット 2014-05-18 10.59.27.png

4
4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?