简体   繁体   中英

Grails domain class validate() returns false but no error

In the below code validate() is returning false but I am not seeing any errors. As the validate method is failing the data is not getting persisted to the database.

if (!stockInstance.validate() && stockInstance.save(flush: true))
 //redirect(action: "show", id: saleInstance.id)
} else {
 println 'stock instance has errors'
 stockInstance.errors.each {
 println it
}
render(view: "edit", model: [saleInstance: saleInstance])
}

This is printing

stock instance has errors
org.springframework.validation.BeanPropertyBindingResult: 0 errors

So where else could the error be.

Sorry was not watching the data properly, It was getting persisted correctly.

You have !stockInstance.validate() but it should be stockInstance.validate() .

!stockInstance.validate() && stockInstance.save(flush: true) can never be true since save() calls validate() so it becomes !stockInstance.validate() && stockInstance.validate()

It's not persisting since the save() call doesn't happen since the validate() call returns true (so the 1st check is false).

Since save() calls validate() I'm not sure why you have the extra call there though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM