Home Service validate domain model
Reply: 1

Service validate domain model

Derrick
1#
Derrick Published in 2017-11-30 09:49:33Z

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)

James Kleeh
2#
James Kleeh Reply to 2017-11-30 15:56:15Z

Inject the messageSource

MessageSource messageSource

Then use it

messageSource.getMessage(...)

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.344667 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO