问题描述
在我的 Grails 1.3.7 项目中,我有一个这样的域类:
In my Grails 1.3.7 project I have a domain class like this:
class User {
String login
String password
String name
String passwordConfirmation
static constraints = {
login unique:true, blank:false, maxSize:45
password password:true, blank:false, size:8..45,
matches: /(?=.*d)(?=.*[a-z])(?=.*[A-Z])(?!.*s).*/
name blank:false, maxSize:45
passwordConfirmation display:true, password:true, validator: { val, obj ->
if (!obj.properties['password'].equals(val)) {
return ['password.mismatch']
}}
}
static transients = ['passwordConfirmation']
String toString() {
name
}
}
我正在使用脚手架进行相应的创建/编辑操作.
And I'm using scaffold for the corresponding create/edit actions.
我的问题是,即使我标记了要显示的 passwordConfirmation 约束,它也不会显示在脚手架视图中.有什么我缺少的东西可以显示瞬态属性吗?可能吗?
My problem is that even if I marked passwordConfirmation constraint to be displayed, it isn't shown at the scaffold views. Is there something that I'm missing to make transient properties to be displayed? Is it possible?
谢谢
推荐答案
默认情况下,grails 不会在视图中为瞬态属性创建字段.您可以在每个视图上手动添加它们,或者如果您有很多它们并且正在使用脚手架视图,您可以执行以下操作:
By default grails doesn't create the fields in views for transient properties. You could manually add them on each view or if you have a lot of them and are using the scaffolded views you could do the following:
安装视图模板:
grails InstallTemplates
然后在src/templates/scaffolding中打开相关模板
Then open the relevant templates in src/templates/scaffolding
并修改以下行:
persistentPropNames = domainClass.persistentProperties*.name
到
persistentPropNames = domainClass.properties*.name
对于每个模板.这有点麻烦,但它应该可以工作,您可以进一步编辑模板以包含/排除您喜欢的任何属性.
for each of the templates. This is a bit of a bodge, but it should work and you can further edit the template to include/exclude any properties you like.
这篇关于在脚手架视图中显示域瞬态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!