Search code examples
grailsgrails-4

how to bind multi field date to grails 4 date?


I have a simple Book domain.

class Book {

    String title
    Date published


    static constraints = {
    }
}

I have created a simple form for book creation

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title></title>
</head>

<body>


<g:form action="save">


    Title: <g:textField name="title"></g:textField>

    <br>



    Published:


    <input type="text" name="startDateTime_dayMonthYear" id="startDate" value="01/06/2022" maxLength="10" size="8" pattern="^\d{1,2}/\d{1,2}/\d{4}$" placeholder="mm/dd/yyyy" />



    <input type="text" name="startDateTime_hourMin" id="startDateTime_hourMin" value="8:00" maxLength="5" size="3.5" pattern="^([1-9]|[1][0-2]):[0-5][0-9]$" autocomplete="off" placeholder="hh:ss" />
    <select name="startDateTime_meridian" id="startDateTime_meridian" >
        <option value="AM" selected="selected" >AM</option>
        <option value="PM" >PM</option>
    </select>


    <select name="startDateTime_timeZone" id="startDateTime_timeZone" >
        <option value="US/Hawaii" >(GMT-10:00) US/Hawaii</option>
        <option value="US/Alaska" >(GMT-09:00) US/Alaska</option>
        <option value="US/Pacific" >(GMT-08:00) US/Pacific</option>
        <option value="US/Arizona" >(GMT-07:00) US/Arizona</option>
        <option value="US/Mountain" selected="selected" >(GMT-07:00) US/Mountain</option>
        <option value="US/Central" >(GMT-06:00) US/Central</option>
        <option value="US/Eastern" >(GMT-05:00) US/Eastern</option>
        <option value="US/East-Indiana" >(GMT-05:00) US/East-Indiana</option>
    </select>







    <br>


    <g:submitButton name="Submit"></g:submitButton>

</g:form>



</body>
</html>

create and save actions are as follows

def create(){

}


def save(){


    def book = new Book()

    bindData(book, params)

    book.save()

    redirect(actionName: "index")


}

For referance the form looks like this

enter image description here

Is there a way to bind the published date property with this multi field date structure? I know the way to bind date in grails 4 from single text field but with multi fields like this i cannot find resource on how to do it.

I am using grails 4.0.10. I appreciate any help. Thanks!


Solution

  • Is there a way to bind the published date property with this multi field date structure?

    If you mean by way of using the data binder built into Grails, I think the answer is no. I don't think our binder has support for pulling in the timezone. The StructuredEditor at https://github.com/grails/grails-core/blob/bc9dd3402ac6a78b0b30d4b9333cbc55ff50029d/grails-web-common/src/main/groovy/org/grails/web/binding/StructuredDateEditor.java knows about year, month, day, hour and minute.