Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5940027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:54:39+00:00 2026-05-22T15:54:39+00:00

I have this controller which performs me a multiple domain databinding class, and it

  • 0

I have this controller which performs me a multiple domain databinding class, and it is working as i want. But i would like some help about the error messages. Is it possible to display error messages from multiple domain classes? If so, how should be the code both is the view and in the controller?

class CarroMovel {
    String move
    String rodas
    String espelhos
    Carro carro

    static hasMany = [carros: Carro]
    static belongsTo = Carro

    static constraints = {
        move(nullable:false, blank:false)
    }

    static mapping = {
        version false
    }
}

class Carro {

String name
String marca
String matricula

static constraints = {
    name(nullable:false, blank:false)
}

static mapping = {
    version false
}

}


def save3 = {

        def carroInstance = new Carro( )
        def carroMovelInstance = new CarroMovel( )

        carroInstance.name = params.carro.name
        carroInstance.marca = params.carro.marca
        carroInstance.matricula = params.carro.matricula

        carroMovelInstance.move = params.carroMovel.move
        carroMovelInstance.rodas = params.carroMovel.rodas
        carroMovelInstance.espelhos = params.carroMovel.espelhos

        carroInstance.save(failOnError: true)

        carroMovelInstance.carro = carroInstance
        carroMovelInstance.save(failOnError: true)

    }

<g:form controller="carro" action="save3">
<h1>Add New Carro Record</h1>
<p>Basic Information</p>

<label>Name
<span class="small">Add your name</span>
</label>
<input type="text" name="carro.name" value="${carroInstance?.name}" /><br>

<label>Marca
<span class="small">Add your name</span>
</label>
<input type="text" name="carro.marca" value="${carroInstance?.marca}" /><br

  <label>Matricula
<span class="small">Add your name</span>
</label>
<input type="text" name="carro.matricula" value="${carroInstance?.matricula}" /><br>

<label>Move
<span class="small">Add your name</span>
</label>
<input type="text" name="carroMovel.move" value="${carroMovelInstance?.move}" /><br>

<label>Rodas
<span class="small">Add your name</span>
</label>
<input type="text" name="carroMovel.rodas" value="${carroMovelInstance?.rodas}" /><br>

<label>Espelho
<span class="small">Add your name</span>
</label>
<input type="text" name="carroMovel.espelhos" value="${carroMovelInstance?.espelho}" /><br>

<g:submitButton name="save" value="Save" id="addConference"/>


<div class="spacer"></div>
  </g:form>

                <g:hasErrors bean="${carroInstance}">
            <div class="errors">
                <g:renderErrors bean="${carroInstance}" as="list" />
            </div>
            </g:hasErrors>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:54:40+00:00Added an answer on May 22, 2026 at 3:54 pm

    Grails Validation Errors Primer

    The errors for a domain object are stored in an errors property that is added to the object after it is validated. This property is an implementation of the Spring Errors interface.

    You can get the errors either by calling the methods of this interface directly, e.g. to display the errors for the move field of a Carro instance:

    List<FieldError> moveErrors = carroMovelInstance.errors?.getFieldErrors('move')   
    

    To get the error messages for each error, you’ll need a referrence to the messageSource bean created by Grails. You could get the message for each of the above errors with:

    List<String> errorMessages = moveErrors.collect {error -> 
        messageSource.getMessage(error, Locale.default) 
    }
    

    Alternatively, Grails provides the eachError and renderErrors tags that simplify displaying errors and their corresponding messages in a GSP.


    Specific Problems With Your Code

    In your controller code, an exception will be thrown whenever save fails due to validation errors, so the view has no opportunity to display the errors. To fix this, change the controller so that it returns the domain objects (along with their errors) when saving fails

    def save3 = {
    
        def carroInstance = new Carro( )
        def carroMovelInstance = new CarroMovel( )
    
        carroInstance.name = params.carro.name
        carroInstance.marca = params.carro.marca
        carroInstance.matricula = params.carro.matricula
    
        carroMovelInstance.move = params.carroMovel.move
        carroMovelInstance.rodas = params.carroMovel.rodas
        carroMovelInstance.espelhos = params.carroMovel.espelhos
    
        // I'm assuming in the code below that the view that displays the form is 'create.gsp'
        if (!carroInstance.save()) {
            render view: 'create', model : [carro: carroInstance, carroMovel: carroMovelInstance]
            return
        }
    
        carroMovelInstance.carro = carroInstance
    
        if (!carroMovelInstance.save()) {
            render view: 'create', model : [carro: carroInstance, carroMovel: carroMovelInstance]
        }
    
    }
    

    The GSP also needs to be changed to display these errors using either the Errors API directly or one of the Grails tags (see above)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a controller which loads the view like this: class A extends Controller{
I have an action like this: public class News : System.Web.Mvc.Controller { public ActionResult
In our code we used to have something like this: *(controller->bigstruct) = ( struct
I have some code which is common to a number of my controller actions
Having this route: map.foo 'foo/*path', :controller => 'foo', :action => 'index' I have the
I have this code in jQuery, that I want to reimplement with the prototype
Say you have several webparts, one as a controller and several which take information
I have a controller (address /Home/Test see below) which definately responds to get request
I want to have generalised email templates. Currently I have multiple email templates with
So I have this basic action, which is accessed by ajax call, and fetches

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.