gpt4 book ai didi

validation - Grails:更改属性名称 “errors”

转载 作者:行者123 更新时间:2023-12-02 15:21:15 24 4
gpt4 key购买 nike

我有一个看起来像这样的类(class)

@Validateable
class StudentBean{
def String name;
def String age;
def String address;

def List<ErrorBean> errors = [];
static constraints = {
age nullable : false, validator : { val, obj, errors->
if(val<10)
errors.rejectValue("age", "student.age.notQualified.message", [val] as Object[], "Student not qualified.");
}
}
}

您可以看到我在StudentBean(列表)中具有一个错误属性。我意识到变量名与存储bean错误的错误有冲突。我所做的就是将闭合更改为类似的内容
age nullable : false, validator : { val, obj, errorsValue->
if(val<10)
errorsValue.rejectValue("age", "student.age.notQualified.message", [val] as Object[], "Student not qualified.");
}

但是我无法循环使用errorsValue来获取错误。

当我检查什么错误时,这就是医生说的:

The errors property on domain classes is an instance of the Spring Errors interface. The Errors interface provides methods to navigate the validation errors and also retrieve the original values. https://grails.github.io/grails-doc/latest/guide/validation.html



因此,这是域类的属性,可能已声明但已隐藏。
问题是我可以将错误属性名称更改为其他名称吗?

最佳答案

您不能更改验证错误属性。

其原因归结于Grails背后的哲学。按照惯例,errors存在于所有Grails域类中。使Grails强大的许多因素,不是配置,而是“配置之上的约定”。这样可以确保Grails项目之间的一致性,并使整个框架更易于学习和使用。

关于validation - Grails:更改属性名称 “errors”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35717064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com