Reputation: 431
i have a strange behavior with domain saving , here is the domain:
class Ads {
String adTitle
String adDetails
String duration
Date dateCreated
static belongsTo = [user:Users]
static constraints = {
category (nullable:false)
adTitle (nullable:false, maxSize:100 )
adDetails(nullable:false, maxSize:500 )
duration (inList:["7 days", "14 days", "30 days"],nullable:true)
}
static mapping ={ duration (sqlType:"datetime") }
}
here is the save action in the controller:
@Transactional
def save(Ads adsInstance) {
if (adsInstance == null) {
notFound()
return
}
if (adsInstance.hasErrors()) {
respond adsInstance.errors, view:'create'
return
}
adsInstance.user=Users.get(springSecurityService.currentUserId)
def adCreationDate = new Date()
switch (adsInstance.duration) {//here i'm just modifying the duration
case "7 days":
adsInstance.duration=adCreationDate+7
break
case "14 days":
adsInstance.duration=adCreationDate+14
break
case "30 days":
adsInstance.duration=adCreationDate+30
break
default:
adsInstance.duration=adCreationDate+7
}
adsInstance.save flush:true
if (adsInstance.isAttached()) //here the return is always not saved
{
println "Saved"
}
else
{
println " not Saved"
}
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.created.message', args: [
message(code: 'ads.label', default: 'Ads'),
adsInstance.id
])
// redirect adsInstance
redirect action:'show' ,id:adsInstance.id
}
'*' { respond adsInstance, [status: CREATED] }
}
}
in the adsInstance.isAttached() i always getting Not saved although adsInstance.save flush:true is called before it , am i missing something ?
Upvotes: 1
Views: 40
Reputation: 2188
You are missing two things:
failOnError: true
with save method.You have added a constraint on duration
field that it can contain values ["7 days", "14 days", "30 days"]
only. But inside switch block you are assigning it date type values which won't pass the validation. Secondly, according to the validation, the sql type for duration
field should not be datetime
. You should either create a custom validatior or use a transient field.
Upvotes: 1