Search code examples
grailsscaffolding

grails - scafold update prevent from saving


I'm using grails 2.4.4 and default update method generated by scaffolding. Let's say it is:

@Transactional
def update(Club clubInstance) {
    if (clubInstance == null) {
        notFound()
        return
    }

    if (clubInstance.hasErrors()) {
        respond clubInstance.errors, view:'create'
        return
    }

    clubInstance.save flush:true

    request.withFormat {
        form {
            flash.message = message(code: 'default.created.message', args: [message(code: 'clubInstance.label', default: 'Club'), clubInstance.id])
            redirect clubInstance
        }
        '*' { respond clubInstance, [status: CREATED] }
    }
}

I would like to prevent saving under some circumstance. But I found out that I could replace whole method with:

@Transactional
def save(Club clubInstance) {
    redirect action:"index"
}

And it still saves data. Only if I remove Club clubInstance from arguments it stops to save data to db. Could anyone tell me why it happens and how can I control saving?


Solution

  • Thanks for comments. It is what I want - conditional saving.

    @Transactional
    def save(Club clubInstance) {
        if(testSomeCondition())
            clubInstance.save flush:true
        else
            clubInstance.discard()
        redirect action:"index"
    }