I have a Grails 3.1.2 application
One of my domain classes is Goal
import java.time.LocalDateTime
import java.time.temporal.ChronoUnit
class Goal {
String name
String description
LocalDateTime targetDate
Date dateCreated
Date lastUpdated
static constraints = {
name(nullable: false, blank: false)
}
}
When I call validate on my an instance of Goal I get:
How can I change the validation so it doesn't need this constructor?
How can I change the validation so it doesn't need this constructor?
You can't, but the validation isn't really the problem. The data binder is the problem. The default binder does not yet have built in support for LocalDateTime
.
You can register your own converter like this:
Converter:
// src/main/groovy/demo/MyDateTimeConverter.groovy
package demo
import grails.databinding.converters.ValueConverter
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
class MyDateTimeConverter implements ValueConverter {
@Override
boolean canConvert(Object value) {
value instanceof String
}
@Override
Object convert(Object value) {
def fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
LocalDateTime.parse(value, fmt)
}
@Override
Class<?> getTargetType() {
LocalDateTime
}
}
Register it as a bean:
// grails-app/conf/spring/resources.groovy
beans = {
myConverter demo.MyDateTimeConverter
}