我有这个控制器,它执行一个多域数据绑定类,它正在我想要的工作。但我想对错误消息提供一些帮助。是否可以显示来自多个域类的错误消息?如果是这样,那么代码应该是视图还是控制器?
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
}
}
类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>
答案 0 :(得分:1)
域对象的错误存储在errors
属性中,该属性在验证后添加到对象中。此属性是Spring Errors接口的实现。
您可以通过直接调用此接口的方法来获取错误,例如显示move
实例的Carro
字段的错误:
List<FieldError> moveErrors = carroMovelInstance.errors?.getFieldErrors('move')
要获取每个错误的错误消息,您需要参考Grails创建的messageSource
bean。您可以通过以下方式获取上述每个错误的消息:
List<String> errorMessages = moveErrors.collect {error ->
messageSource.getMessage(error, Locale.default)
}
或者,Grails提供eachError
和renderErrors
标记,以简化在GSP中显示错误及其相应的消息。
在您的控制器代码中,只要save
因验证错误而失败,就会抛出异常,因此视图无法显示错误。要解决此问题,请更改控制器,以便在保存失败时返回域对象(及其错误)
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]
}
}
还需要更改GSP以使用Errors
API直接显示这些错误或使用其中一个Grails标记(参见上文)