0

我有一个简单的域实体:

package shoesshop

class Brand {

    String name
    String description
    String logoImageURL

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

当我尝试使用 null 值作为名称保存新品牌​​时,我想呈现一条消息,上面写着“必须指定名称”。

我尝试将属性添加到messages.properties

brand.name.nullable=Brand name must be specified  

但它不会自动拾取。我应该如何从那里检索它?我看了看,brand.errors它只包含一条默认消息

Property [{0}] of class [{1}] cannot be null.

它还包含一组错误代码,其中一个是brand.name.nullable.

4

3 回答 3

2

你有没有尝试过?:

if(brand.name==null)
{
    flash.message = message(code: 'brand.name.nullable',default:'Brand name must be specified.');
    render(view: "create", model: [brand:brand])
    return
}

您可以尝试将“default.null.message”的消息(不知道这将如何与您的整个应用程序一起执行)更改为

{1} {0} must be specified
于 2012-05-22T19:12:08.773 回答
1

当您没有可空约束时,会出现可空错误消息,这对我来说很奇怪。在空白和可为空的文档中,它清楚地表明它nullable不同于blank并具有单独的消息。

尝试

brand.name.blank

我似乎还必须添加.error才能使事情正常工作:

brand.name.blank.error or brand.name.nullable.error
于 2012-05-22T21:07:29.500 回答
0

我正在使用 Grails 2.2.3。对我来说,它在我使用时有效

brands.name.blank=The name cannot be empty

现在,如果我在收到此错误后刷新页面“.../appname/controllername/save”,我会得到

Property [name] of class [class ...] cannot be null

此消息可以更改为

brands.name.nullable=The name cannot be null (do not refresh the page!!!)
于 2013-07-14T18:55:43.600 回答