How do I bind a checkbox to a boolean in Play! framework

前端 未结 3 1512
轮回少年
轮回少年 2021-02-19 19:34

An app I\'m working on using the Play! Framework has an object called gift with a boolean property, called Taken. How do I show the state of this value as a checkbox on my view

3条回答
  •  不知归路
    2021-02-19 20:21

    The accepted answer is actually not 100% correct as it doesn't handle the "unchecked" case. To handle both cases you need a hidden field:

    
    
    

    Note that the placement seems to be important, so the hidden field must be after the checkbox.

    Writing a custom template tag for this, makes it easy not to forget the hidden input (put this into views/tags/checkbox.html):

    
    
    

    Then call this template like this:

    #{checkbox id:'gift_Taken', name: 'gift.Taken', checked: gift.Taken /}
    

    See also the related discussion on the play framework list: https://groups.google.com/forum/?fromgroups=#!topic/play-framework/HygQuYF3a8E

提交回复
热议问题