Skip to content

Instantly share code, notes, and snippets.

@robfletcher
Created February 26, 2010 13:25
PropertyEditor implementation used for binding to Grails domain objects by unique property value
import java.beans.PropertyEditorSupport
import org.apache.commons.lang.StringUtils
class DomainClassLookupPropertyEditor extends PropertyEditorSupport {
Class domainClass
String property
String getAsText() {
value."$property"
}
void setAsText(String text) {
value = domainClass."findBy${StringUtils.capitalize(property)}"(text)
if (!value) {
value = domainClass.newInstance((property): text)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment