LoginSignup
2
2

More than 5 years have passed since last update.

RevelでFormの配列を扱う

Last updated at Posted at 2015-07-28

Structの配列初期化

type Book struct {
    Name []string
}
bookNum := 2
books := Book{Name: make([]string, bookNum)}
books := Book{
    Name: []string{"hoge", "fuga"},
}

RevelのTemplateでFormを作る

{{if gt .bookNum 2 }}
{{$sa := index .books.Name 2}}
{{with $field := field "books.Name[2]" .}}
<p class="{{$field.ErrorClass}}">
    <strong>Check In Date:</strong>
    <input type="text" size="10" name="{{$field.Name}}" class="datepicker" value="{{firstof $field.Flash $sa}}">
    * <span class="error">{{$field.Error}}</span>
</p>
{{end}}
{{end}}

RevelでValidationしてみる

for index, book := range books.Name {
    keyName := "books.Name[" + strconv.Itoa(index) + "]"
    c.Validation.Required(book).Key(keyName).Message("Your name is required!")
    c.Validation.MinSize(book, 3).Key(keyName).Message("Your name is not long enough!")
}
2
2
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
2
2