Skip to content

Commit

Permalink
Merge pull request #391 from codeconsole/6.0.x-localdate
Browse files Browse the repository at this point in the history
LocalDate and LocalDateTime support
  • Loading branch information
codeconsole authored Jan 12, 2025
2 parents 03b36e3 + 67b5e07 commit 8ad65b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import org.springframework.web.servlet.LocaleResolver
import jakarta.servlet.http.HttpServletRequest
import java.sql.Blob
import java.text.NumberFormat
import java.time.LocalDate
import java.time.LocalDateTime

import static FormFieldsTemplateService.toPropertyNameFormat

Expand Down Expand Up @@ -712,7 +714,7 @@ class FormFieldsTagLib {
return renderAssociationInput(model, attrs)
} else if (oneToMany) {
return renderOneToManyInput(model, attrs)
} else if (model.type in [Date, Calendar, java.sql.Date, java.sql.Time]) {
} else if (model.type in [Date, Calendar, java.sql.Date, java.sql.Time, LocalDate, LocalDateTime]) {
return renderDateTimeInput(model, attrs)
} else if (model.type in [byte[], Byte[], Blob]) {
return g.field(attrs + [type: "file"])
Expand All @@ -726,7 +728,7 @@ class FormFieldsTagLib {


CharSequence renderDateTimeInput(Map model, Map attrs) {
attrs.precision = model.type == java.sql.Time ? "minute" : "day"
attrs.precision = model.type in [java.sql.Time, LocalDateTime]? "minute" : "day"
if (!model.required) {
attrs.noSelection = ["": ""]
attrs.default = "none"
Expand Down Expand Up @@ -915,6 +917,8 @@ class FormFieldsTagLib {
case Date:
case java.sql.Date:
case java.sql.Time:
case LocalDate:
case LocalDateTime:
g.formatDate(date: model.value)
break
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import spock.lang.Shared
import spock.lang.Unroll

import java.sql.Blob
import java.time.LocalDate
import java.time.LocalDateTime

@Unroll
class DefaultInputRenderingSpec extends AbstractFormFieldsTagLibSpec implements TagLibUnitTest<FormFieldsTagLib> {
Expand Down Expand Up @@ -248,6 +250,8 @@ class DefaultInputRenderingSpec extends AbstractFormFieldsTagLibSpec implements
Calendar | /select name="prop_day"/
java.sql.Date | /select name="prop_day"/
java.sql.Time | /select name="prop_day"/
LocalDate | /select name="prop_day"/
LocalDateTime | /select name="prop_day"/
TimeZone | /<option value="Europe\/London"/
Locale | /<option value="en_GB"/
Currency | /<option value="GBP"/
Expand All @@ -265,6 +269,8 @@ class DefaultInputRenderingSpec extends AbstractFormFieldsTagLibSpec implements
Date | new Date(108, 9, 2) | /option value="2008" selected="selected"/
Calendar | new GregorianCalendar(2008, 9, 2) | /option value="2008" selected="selected"/
java.sql.Date | new java.sql.Date(108, 9, 2) | /option value="2008" selected="selected"/
LocalDate | LocalDate.of(2008, 9, 2) | /option value="2008" selected="selected"/
LocalDateTime | LocalDateTime.of(2008, 9, 2, 0, 0) | /option value="2008" selected="selected"/
java.sql.Time | new java.sql.Time(13, 29, 1) | /option value="13" selected="selected"/
TimeZone | TimeZone.getTimeZone("Europe/London") | /<option value="Europe\/London" selected="selected"/
Locale | Locale.ITALIAN | /<option value="it" selected="selected"/
Expand All @@ -287,10 +293,14 @@ class DefaultInputRenderingSpec extends AbstractFormFieldsTagLibSpec implements
Calendar | true
java.sql.Date | true
java.sql.Time | true
LocalDate | true
LocalDateTime | true
Date | false
Calendar | false
java.sql.Date | false
java.sql.Time | false
LocalDate | false
LocalDateTime | false
}

def "select for a #type.simpleName property has a precision of 'day'"() {
Expand All @@ -308,12 +318,12 @@ class DefaultInputRenderingSpec extends AbstractFormFieldsTagLibSpec implements
!output.contains('select name="prop_minute"')

where:
type << [Date, Calendar, java.sql.Date]
type << [Date, Calendar, java.sql.Date, LocalDate]
}

def "select for a Time property has a precision of 'minute'"() {
given:
def model = [type: java.sql.Time, property: "prop", constraints: null, persistentProperty: basicProperty]
def model = [type: type, property: "prop", constraints: null, persistentProperty: basicProperty]

when:
def output = tagLib.renderDefaultInput(model)
Expand All @@ -324,6 +334,9 @@ class DefaultInputRenderingSpec extends AbstractFormFieldsTagLibSpec implements
output.contains('select name="prop_day"')
output.contains('select name="prop_hour"')
output.contains('select name="prop_minute"')

where:
type << [java.sql.Time, LocalDateTime]
}

def "select with Locale,TZ,currency for #{required ? 'a required' : 'an optional'} #type.simpleName property #{required ? 'does not have' : 'has'} a no-selection option"() {
Expand Down

0 comments on commit 8ad65b0

Please sign in to comment.