Search code examples
grailsgrails-orm

Service validate domain model


I face a problem with the message method from the domain validation from the service. Does anyone knows how to fix this? Below is the service method. When my controller is calling: glossaryService.post(params, 'save').

@Transactional
class GlossaryService {

//  Saving glossary
def post( def params,def currentURI ) {
    params.status = params.status ? Status.PUBLISHED.value() : Status.DRAFT.value()

    def result = [success: false, message: ""]
    def glossary = null

    if (params.action == 'save') {
        glossary = new Glossary(params)
    } else if (params.action == 'update') {
        glossary = Glossary.get(params.id)
        glossary.name = params?.name
        glossary.description = params?.description
        glossary.glossaryTerm = params?.glossaryTerm
        glossary.status = params?.status
    }

    if (glossary.validate()) {
        result = glossary.save(flush: true, failOnError: true)
    }

    if (glossary.hasErrors()) {
        transactionStatus.setRollbackOnly()

        result.errors = glossary.errors
        def errors = glossary.errors.allErrors.collect {
            message(error: it)
        }

        respond glossary.errors, view: '/admin/glossary/'+currentURI, model: [
                glossary: params
        ]
        return
    }

    return result
}
}

The error it prompts is:

Class: groovy.lang.MissingMethodException

Message: No signature of method: com.content.GlossaryService.message() is applicable for argument types: (java.util.LinkedHashMap) values: [[error:Field error in object 'com.content.Glossary' on field 'name': rejected value [null]; codes [com.content.Glossary.name.nullable.error.com.content.Glossary.name,com.content.Glossary.name.nullable.error.name,com.content.Glossary.name.nullable.error.java.lang.String,com.content.Glossary.name.nullable.error,glossary.name.nullable.error.com.content.Glossary.name,glossary.name.nullable.error.name,glossary.name.nullable.error.java.lang.String,glossary.name.nullable.error,com.content.Glossary.name.nullable.com.content.Glossary.name,com.content.Glossary.name.nullable.name,com.content.Glossary.name.nullable.java.lang.String,com.content.Glossary.name.nullable,glossary.name.nullable.com.content.Glossary.name,glossary.name.nullable.name,glossary.name.nullable.java.lang.String,glossary.name.nullable,nullable.com.content.Glossary.name,nullable.name,nullable.java.lang.String,nullable]; arguments [name,class com.content.Glossary]; default message [Property [{0}] of class [{1}] cannot be null]]] Possible solutions: isCase(java.lang.Object)


Solution

  • Inject the messageSource

    MessageSource messageSource

    Then use it

    messageSource.getMessage(...)